python - 我如何获得第二个表类?
问题描述
我正在尝试使用 BeautifulSoup 在维基百科页面中查找表。我知道如何获取第一个表,但如何获取具有相同类别的第二个表(标准普尔 500 成分股列表的最新更改)wikitable sortable
?
我的代码:
import bs4 as bs
import requests
url='https://en.wikipedia.org/wiki/List_of_S%26P_500_companies'
r=requests.get(url)
url=r.content
soup = bs.BeautifulSoup(url,'html.parser')
tab = soup.find("table",{"class":"wikitable sortable"})
解决方案
您可以使用soup.find_all
和访问最后一个表。由于只有两个table
标签wikitable sortable
作为其类,结果列表中的最后一个元素将是“最近更改”表:
soup.find_all("table", {"class":"wikitable sortable"})[-1]
推荐阅读
- android - 如何在我的 android 应用程序中创建一个页面,显示 Play 商店中我的应用程序的当前评论和评分列表
- python - 如何将列表中的日期与今天的日期进行比较?
- r - Tidyverse:case_when() 没有返回正确的值
- css - 如何防止 CSS Grid 井喷?
- c - 宏映射函数及其地址
- html - 在 Gmail 和 Outlook 中隐藏内容不起作用
- ruby-on-rails - 我可以一次检查多个动作参数吗
- python - 如何用beautifulsoup做简单的分页循环
- api - IBM API Connect 云 Oauth API
- javascript - 当相应部分在屏幕上时,将活动类添加到 li