python - 在 Python 中使用某些文本抓取 HTML 表格

标签 python html web-scraping html-table

我正在尝试使用 python 网络抓取 HTML 表格。 HTML 页面中有很多表格,但我只想抓取某个表格。我正在使用漂亮的汤来做这个网页抓取。

我的代码是这样的:

page = get("http://uobgoldprice.com/history/2018/September/10/")
html = BeautifulSoup(page.content, 'html.parser')

for p in html.select('tr'):
    if p.text == "ARGOR CAST BAR":
        print (p.text)

我只想要“截至 2018 年 9 月 10 日星期一的汇率”的表格。

我该怎么做?

最佳答案

您需要找到包含文本的元素和作为表格的父元素:

import re
import requests
from bs4 import BeautifulSoup

page = requests.get("http://uobgoldprice.com/history/2018/September/10/")
html = BeautifulSoup(page.content, 'html.parser')

element = html.find(text=re.compile('Rate as at Monday, 10 September 2018'))
print(element.findParent('table'))

关于python - 在 Python 中使用某些文本抓取 HTML 表格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52336057/

相关文章:

javascript - 用于弹出窗口内分页和网页抓取的动态 CasperJS 步骤数

python - Pyinstaller 可执行文件在隐藏时不会运行进程

html - 我的 'menu' 类没有粘在屏幕顶部,当我进行填充时高度自动不起作用

html - 如何在悬停时添加文本

python - 如何使用 Python 从网站中提取多个独立嵌套的 JSON 对象和键

python - beautifulsoup - 删除一行代码

python - 将 JSON 模式转换为 python 类

python - tf.slice 和 tf.strided_slice

python - 错误 : Command '[' dot', '-V' ]' 返回非零退出状态 -5 使用 Iris 时

javascript - 无法使用innerHTML和innerText更改标签内容