首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >查找具有相同类名的多个表,Python webscraping

查找具有相同类名的多个表,Python webscraping
EN

Stack Overflow用户
提问于 2020-04-25 12:29:36
回答 1查看 58关注 0票数 0

我正在尝试使用Python4和BeautifulSoup抓取具有相同类名的多个表。

代码语言:javascript
复制
from bs4 import BeautifulSoup
import csv

standingsURL = "https://efl.network/index/efl/Standings.html"
standingsPage = requests.get(standingsURL)
standingsSoup = BeautifulSoup(standingsPage.content, 'html.parser')
standingTable = standingsSoup.find_all('table', class_='Grid')
standingTitles = standingTable.find_all("tr", class_='hilite')
standingHeaders = standingTable.find_all("tr", class_="alt")

但是,在运行此命令时,它会显示以下错误

代码语言:javascript
复制
Traceback (most recent call last):
  File "C:/Users/user/Desktop/program.py", line 15, in <module>
    standingTitles = standingTable.find_all("tr", class_='hilite')
  File "C:\Users\user\AppData\Local\Programs\Python\Python37-32\lib\site-packages\bs4\element.py", line 2128, in __getattr__
    "ResultSet object has no attribute '%s'. You're probably treating a list of elements like a single element. Did you call find_all() when you meant to call find()?" % key
AttributeError: ResultSet object has no attribute 'find_all'. You're probably treating a list of elements like a single element. Did you call find_all() when you meant to call find()?

如果我使用standingTable = standingsSoup.find('table', class_='Grid')更改standingTable = standingsSoup.find_all('table', class_='Grid')

它可以工作,但在我尝试获取两个表的数据时,它只提供其中一个表的数据

EN

回答 1

Stack Overflow用户

发布于 2020-04-26 09:07:39

尝尝这个。

代码语言:javascript
复制
from simplified_scrapy import SimplifiedDoc,req,utils
standingsURL = "https://efl.network/index/efl/Standings.html"
standingsPage = req.get(standingsURL)
doc = SimplifiedDoc(standingsPage)
standingTable = doc.selects('table.Grid')
standingTitles = standingTable.selects("tr.hilite")
standingHeaders = standingTable.selects("tr.alt")
print(standingTitles.tds.text)

结果:

代码语言:javascript
复制
[[['Wisconsin Brigade', '10', '3', '0', '.769', '386', '261', '6-1-0', '4-2-0', '3-2-0', '3-2-0', 'W4'], ...

这里有更多的例子。https://github.com/yiyedata/simplified-scrapy-demo/tree/master/doc_examples

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/61420988

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档