【发布时间】:2020-08-30 04:20:28
【问题描述】:
我正在尝试使用 Beautiful Soup 抓取维基百科。我想获取里面的文本,但只获取带有特定标题文本的行的内容。
例如: 我想获取 Alan Turing 从https://en.wikipedia.org/wiki/Alan_Turing 获得的奖项列表
我需要的信息在右表中,在表头对应的表数据中,带有文本Awards。如何获得奖项列表?
我尝试遍历表格行并检查表格标题是否等于“Awards”,但我不知道如何停止循环以防表格中没有“Awards”标题。
testurl = "https://en.wikipedia.org/wiki/Alan_Turing"
page = requests.get(testurl)
page_content = BeautifulSoup(page.content, "html.parser")
table = page_content.find('table' ,attrs={'class':'infobox biography vcard'})
while True:
tr = table.find('tr')
if tr.find('th').renderContents() == 'Awards':
td = tr.find('td')
break
print(td)
【问题讨论】:
-
你能展示一下你尝试过的东西吗?
标签: python web-scraping beautifulsoup