Поиск нескольких таблиц с одинаковым именем класса, Python webscraping - PullRequest
0 голосов
/ 25 апреля 2020

Я пытаюсь очистить несколько таблиц с одним и тем же именем класса, используя BeautifulSoup 4 и Python.

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")

Однако при запуске это выдает ошибку

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_all('table', class_='Grid') на standingTable = standingsSoup.find('table', class_='Grid')

, это работает, но дает только данные одной из таблиц, пока я пытаюсь получить данные обеих

1 Ответ

0 голосов
/ 26 апреля 2020

Попробуйте это.

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)

Результат:

[[['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

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...