提问人:tutizeri 提问时间:7/20/2020 更新时间:7/20/2020 访问量:22
我的代码在维基百科中找不到表格
My code doesn't finds a table in Wikipedia
问:
我试图抓住这个维基百科页面上的最后一张表(标题为“Registro de los casos”)
使用此 Python 3.7 代码
import requests
from bs4 import BeautifulSoup, NavigableString, Tag
def webcrawler():
url = "https://es.wikipedia.org/wiki/Pandemia_de_enfermedad_por_coronavirus_de_2020_en_Argentina"#Cronolog%C3%ADa"
page = requests.get(url)
soup = BeautifulSoup(page.text,"html.parser")
tables = soup.findAll("table", class_='wikitable')[0]
#print(tables)
for table in tables:
if isinstance(table, NavigableString):
continue
ths = table.find_all('th')
headings = [th.text.strip() for th in ths]
print(headings)
webcrawler()
但它只找到第一个表,而不是最后一个表。我做错了什么?
答:
1赞
Beek
7/20/2020
#1
设置为 返回的第一个项目。如果取出,则将具有该类的所有表写入 tables 变量tables
soup.findAll("table", class_='wikitable')[0]
[0]
评论
tables
soup.findAll("table", class_='wikitable')[0]
[0]
tables