python-3.x - 我的代码在 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()
但它只找到第一个表,而不是最后一个。我究竟做错了什么?
解决方案
您设置tables
为返回的第一项soup.findAll("table", class_='wikitable')[0]
。如果您取出,[0]
则将具有该类的所有表写入表变量
推荐阅读
- docker - GitLab Docker Runner 重用已安装的软件层
- c# - C# - 从客户端检查 TCP/IP 套接字状态
- node.js - 由于 MongoDB,Heroku 中的应用程序错误?
- c - pthread_create 我应该使用 & 还是不使用?(C)
- wso2 - 在 WSO IS 5.9 中使用 JDBCUserStoreManager 的辅助自定义用户存储
- java - 基于配置文件的Spring数据中实体的附加过滤器
- electron - 将使用 Electron 构建的 APPX 发布到 Windows Store 和 Desktop Bridge 程序
- swift - 'characters' 不可用:请直接使用字符串。我有这个问题
- ruby-on-rails - 如何将 QR 码存储在从 rqrcode 生成的 Rails 中
- r - 计算和分配变量的连续出现