python - 尝试使用 python 和 bs4 从特定 'a' s 中抓取所有 'td' 文本

标签 python html web-scraping beautifulsoup

我正在尝试抓取https://www.betexplorer.com/soccer/england/premier-league/fixtures/提取“a”标记中包含的文本,特别是在类为“table-main”的表中,然后针对其中的每一行。第一个 td 包含带有两个团队名称的文本,以及 td 类“h-text-left”。不确定问题是否出在我的循环上,但我收到的错误消息似乎是我在循环的最后一行中错误地使用了 bs4。

我可以使用类“table-main”抓取表中的每个 tr,然后使用类“h-text-left”抓取每个 td。不过,当我试图单独提取“a”元素时,我遇到了死胡同,甚至连“a”文本也没有。

import requests
from bs4 import BeautifulSoup

headers = {'User-Agent':
           'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.106 Safari/537.36'}

r = requests.get('https://www.betexplorer.com/soccer/england/premier-league/fixtures/', headers=headers)

c = r.content

soup = BeautifulSoup(c)

fixture_table = soup.find('table', attrs = {'class': 'table-main'})

for tr in soup.find_all('tr'):
    match_tds = tr.find_all('td', attrs = {'class': 'h-text-left'})
    matches = match_tds.find_all('a')

当我尝试查找所有“a”标签时,最后一行出现以下错误:

...     matches = match_tds.find_all('a')
...
Traceback (most recent call last):
  File "<stdin>", line 4, in <module>
  File "C:\Users\Glypt\AppData\Local\Programs\Python\Python36-32\lib\site-packages\bs4\element.py", line 1884, in __getattr__
    "ResultSet object has no attribute '%s'. You're probably treating a list of items like a single item. 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 items like a single item. Did you call find_all() when you meant to call find()?
>>>

最佳答案

您可以使用单个类将其简化为更快的选择器方法。所有链接都具有相同的类名,因此您可以将其传递给列表理解中的 select,以提供所有链接。

import requests
from bs4 import BeautifulSoup
r = requests.get('https://www.betexplorer.com/soccer/england/premier-league/fixtures/')
soup = BeautifulSoup(r.content, 'lxml')
matches = [item['href'] for item in soup.select('.in-match')]
<小时/>

赔率

import requests
from bs4 import BeautifulSoup
r = requests.get('https://www.betexplorer.com/soccer/england/premier-league/fixtures/')
soup = BeautifulSoup(r.content, 'lxml')
odds = [item['data-odd'] for item in soup.select('.table-main__odds [data-odd]')]
print(odds)

关于python - 尝试使用 python 和 bs4 从特定 'a' s 中抓取所有 'td' 文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55583052/

相关文章:

python - 子进程在自己的线程中

python - 将列表组合成一个可以重复使用的可迭代对象

javascript - 通过 Ratchet (Push.js) 使用缓存页面

python - 检查文本中是否存在大量关键字

python - 使用 Selenium 添加新的 CSS

python - 将 netcdf 文件中的所有变量和维度从 int/float 转换为 double

python - 来自一个多处理管理器的多个队列

javascript - 如何消除从嵌入式闪存更改焦点的可能性

javascript - 返回 Javascript 类内部元素中元素的当前索引

python - 如何使用 beautifulsoup 从 url 中的表返回多个页面的数据