我试图从维基百科页面https://en.wikipedia.org/wiki/Minister_of_Agriculture_(India)的表中获取数据。然而,我无法从rowspan Hers的文章中获取完整的数据:
from bs4 import BeautifulSoup
from urllib.request import urlopen
wiki = urlopen("https://en.wikipedia.org/wiki/Minister_of_Agriculture_(India)")
soup = BeautifulSoup(wiki, "html.parser")
table = soup.find("table", { "class" : "wikitable" })
for row in table.findAll("tr"):
cells = row.findAll("td")
if cells:
name = cells[0].find(text=True)
pic = cells[1].find("img")
strt = cells[2].find(text=True)
end = cells[3].find(text=True)
pri = cells[6].find(text=True)
z=name+'\n'+pic+'\n'+strt+'\n'+end+'\n'+pri
print z
这是这个问题唯一的解决办法。在这里,我将把rowspan,colspan table更改为simple table。 我在这个问题上浪费了很多天,但没有找到简单而好的解决办法。在许多stackoverflow解决方案中,开发人员只抓取文本。但在我的例子中,我也需要url链接。所以,我写了这个代码。 这对我有用
相关问题 更多 >
编程相关推荐