You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用Beautiful Soup爬取维基百科表格返回None的问题求助

问题原因

你代码里指定的表格class包含jquery-tablesorter,但这个类是页面加载完成后由JavaScript动态添加的,静态HTML源码里不存在这个类,所以find方法匹配不到目标表格,返回None。

解决方法

简化class选择器,只保留页面静态源码里实际存在的类:wikitable sortable plainrowheaders,修改后的代码如下:

from bs4 import BeautifulSoup
from urllib.request import urlopen

url = 'https://en.wikipedia.org/wiki/List_of_songs_recorded_by_the_Beatles'
html = urlopen(url) 
soup = BeautifulSoup(html, 'html.parser')            

# 去掉动态添加的jquery-tablesorter类
table = soup.find('table', {'class': 'wikitable sortable plainrowheaders'})
print(table)

或者用CSS选择器的方式定位,代码更简洁:

table = soup.select_one('table.wikitable.sortable.plainrowheaders')
验证方式

你可以右键目标页面选择「查看页面源代码」,搜索wikitable sortable plainrowheaders,就能找到对应的表格标签,确认这些类是静态存在的。

内容的提问来源于stack exchange,提问作者rar9000

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.28 09:04:55