soap.findAll('table',class _ ='wikitable sortable')不起作用

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

我刚写了一个简单的查询来解析html页面中的数据。但是我得到一个空的结果列表,即使页面中存在这样的类。这是我的代码:

from bs4 import BeautifulSoup
import urllib
wiki =      "http://en.wikipedia.org/wiki/List_of_postcode_districts_in_the_United_Kingdom"
header = {'User-Agent': 'Mozilla/5.0'} #Needed to prevent 403 error on Wikipedia`enter code here`
req = urllib.Request(wiki,headers=header)
page = urllib.urlopen(req)
soup = BeautifulSoup(page)
table = soup.find("table", { "class" : "wikitable sortable" })
print (table)
python beautifulsoup
2个回答
0
投票

尝试使用完整的类名,它应该工作:

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

0
投票

我可能过于简单化了,但sortablejquery-tablesorter就像是关键词。

你应该只将class指定为wikitable

© www.soinside.com 2019 - 2024. All rights reserved.