python - 在 Python 中使用某些文本抓取 HTML 表格
问题描述
我正在尝试使用 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'))
推荐阅读
- r - 根据样本向量在数据框的单元格中排列字符
- asp.net - asp.net - 请给我看一个dataview控件的find方法的例子
- python - 密码重置激活链接
- flutter - 应用程序检查登录状态时如何设置加载屏幕?
- ajax - Openvidu 服务器 API REST 与 fetch
- c# - 是否有任何选项可以将构造函数添加到派生自 PSCmdlet 的类中
- python-3.x - 从推特上抓取推文的推文来源标签
- java - Java - 带括号的开关标签
- recursion - Neo4j - 递归模式匹配并返回特定叶节点的递归路径
- sql-server - 使用不同表或不同数据库时的 SQL 性能