为了账号安全,请及时绑定邮箱和手机立即绑定

无法弄清楚为什么 soup.find_all() 返回一个空列表

无法弄清楚为什么 soup.find_all() 返回一个空列表

qq_遁去的一_1 2022-11-01 15:03:09
我是 Python 新手,正在使用 BeautifulSoup 练习网页抓取。我检查了一些类似的问题,例如这个、这个和这个。但是,我仍然陷入我的问题。这是我的代码:import urllib.requestfrom bs4 import BeautifulSouphtml = urllib.request.urlopen("https://en.wikipedia.org/wiki/List_of_largest_recorded_music_markets").read()soup = BeautifulSoup(html, 'html.parser')tbody = soup.find_all('table',{"class":"wikitable plainrowheaders sortable jquery-tablesorter"})首先,我不认为我正在寻找的网页包含类似问题中提到的 java 脚本。我打算提取这些表中的数据,但是当我执行 print(tbody) 时,我发现它是一个空列表。有人可以看看并给我一些提示吗?谢谢你。
查看完整描述

1 回答

?
慕村9548890

TA贡献1884条经验 获得超4个赞

您必须删除 jquery-tablesorter 部分。它是在页面加载后动态应用的,所以如果你包含它,它就不起作用。


这应该有效:


import urllib.request

from bs4 import BeautifulSoup


html = urllib.request.urlopen("https://en.wikipedia.org/wiki/List_of_largest_recorded_music_markets").read()

soup = BeautifulSoup(html, 'html.parser')


tbody = soup.find('table', {"class": "wikitable plainrowheaders sortable"})

print(tbody)


查看完整回答
反对 回复 2022-11-01
  • 1 回答
  • 0 关注
  • 255 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信