使用 Beautiful Soup 抓取维基百科表,但没有返回 'None'

问题描述 投票:0回答:1

一般来说,是网络抓取和编码的新手。对于更有经验的人来说,这可能是一个简单的问题......也许不是......这里是:

尝试从维基百科中抓取表格。我已在 html 中找到该表,并将该信息添加到我的代码中。但是,当我运行它时,我得到“无”返回,而不是确认表已正确定位。

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')            

table = soup.find('table',{'class':'wikitable sortable plainrowheaders jquery-tablesorter'})
print(table)

退货:无

python web-scraping beautifulsoup wikipedia
1个回答
0
投票

从“class”字符串中删除

jquery-tablesorter
- 这个类是由 javascript 添加的,beautifulsoup 看不到它:

from urllib.request import urlopen

from bs4 import BeautifulSoup

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

table = soup.find("table", {"class": "wikitable sortable plainrowheaders"})
print(table)
© www.soinside.com 2019 - 2024. All rights reserved.