python - 无论我尝试什么,BeautifulSoup4 都找不到表
问题描述
我正在尝试同时从网页中抓取 2 个表格。BeautifulSoup 找到第一个表没有问题,但无论我尝试什么都找不到第二个表,这是网页:曲棍球参考:Justin Abdelkader。
它是季后赛标题下方的表格。
这是我的代码。
sauce = urllib.request.urlopen('https://www.hockey-reference.com/players/a/abdelju01/gamelog/2014', timeout=None).read()
soup = bs.BeautifulSoup(sauce, 'html5lib')
table = soup.find_all('table')
print(len(table))
总是打印 1。
如果我打印(汤),并在终端中使用搜索功能,我可以找到 2 个单独的表格标签。我没有看到任何会阻碍 BS4 查找标签的 javascript。我也尝试通过 id 和 class 查找表,即使表的父 div 似乎也无法找到。有谁知道我做错了什么?
解决方案
由于 javascript 加载附加信息
今天requests_html可以加载 html 页面也可以加载 javascript 内容。
pip install requests-html
from requests_html import HTMLSession
session = HTMLSession()
r = session.get('https://www.hockey-reference.com/players/a/abdelju01/gamelog/2014')
r.html.render()
res = r.html.find('table')
print(len(res))
4
推荐阅读
- c# - 我无法在控制台项目中将参考编号的格式设置为“字节”或“短”?
- django - How can i fetch data from django models(database) using celery(asynchronously) from previously existing data
- c - C 编程——如何将文本文件内容放入我的数组中?
- r - 使用 geom_segment 将线添加到 ggplot 时出错
- c# - 处理多个断言,如果没有找到则失败测试
- java - 二元运算符的错误操作数 - 两种类型
- archlinux - Arch linux上的Netatalk给出错误--enable-fhs common not found
- c# - 如何检查一个号码是否包含另一个号码的所有数字?
- r - 水平点图的R plotly高度计算
- sqlite - SQLite:如何将 memvfs 扩展嵌入到合并中?