如何通过背景颜色蟒蛇抓取元素



我发现自己有一段时间面临某个问题。

我希望从网站(https://www.pronosoft.com/fr/parions_sport/resultats-parions-sport-plein-ecran.htm?date=07-02-2020(收集某些数据,但问题是我无法正确定位这些数据。

我要提取的数据如下:

  • 第一列足球比赛的获胜赔率 ( 1 N 2 (

让我解释一下...

如您所见,为了获得足球比赛的赔率,我有一个名为 ==> class="m-s-0 m-c-22 m-376811" 的类

M-S-0 指的是足球比赛 M-C-22 指的是 Compet,而 M-376811 指的是我们在"值"中看到的特定比赛 = 376811

获胜赔率由灰色背景表示,灰色背景可在具有多个名称(如res_c res_baisse或res_c或res_c res_hausse等(的类别中找到

我正在寻找如何使用背景颜色恢复这些元素,因为在我看来这是最有效的方法。

我尝试了以下代码:

import requests
from bs4 import BeautifulSoup
url = 'https://www.pronosoft.com/fr/parions_sport/resultats-parions-sport-plein-ecran.htm?date=07-02-2020'
r = requests.get(url)
soup = BeautifulSoup(r.text, 'html.parser')
soup.find_all('res_c', {"style":"background-color:#636364"})

但是什么也没找到,它会返回一个空列表,你对如何进行有想法吗,提前谢谢

马上:

我现在有这样的东西

odds = []
match = []
for match_row_tag in match_list_tag.table.tbody.findChildren('tr'):
# .find() will choose only the first result from the row
winning_odd_tag = match_row_tag.find('span', {'class':'res_c'})
sport_foot_tag = match_row_tag.find('span', {'class':'sport football'})
soup_foot = match_row_tag.find('a', {'class':'infos'})
match = []
if sport_foot_tag in soup_foot not in match and sport_foot_tag in soup_foot is not None:
match.append(sport_foot_tag.parent.get_text().split("n")[0])
for matches in set(match):
print(winning_odd_tag.string, sport_foot_tag.parent.get_text().split("n")[0])
odds.append(winning_odd_tag.string)
else:
pass

像这样:

3,90 Everton - Leicester
1,23 Everton - Leicester
4,30 Everton - Leicester
1,22 CefnDruidsFC-TheNewSaints
1,42 Connah's Quay-Bala Town
1,10 Connah's Quay-Bala Town
3,40 Llandudno FC-Caernarfon
1,18 Llandudno FC-Caernarfon
2,85 Derby - Middlesbrough
1,09 Derby - Middlesbrough
1,28 Derby - Middlesbrough

但问题是我希望匹配项的名称只出现一次,以便只从我的行中获取第一个值

有人知道为什么这没有按预期工作?

这将遍历表格行并找到第一个以"res_c"作为类的 span 元素,这是第一列中具有灰色背景颜色的奇数:

match_list_tag = soup.find('div', {"id":"match-list"})
for match_row_tag in match_list_tag.table.tbody.findChildren('tr'):
# .find() will choose only the first result from the row
winning_odd_tag = match_row_tag.find('span', {'class':'res_c'})
if winning_odd_tag:
print(winning_odd_tag.string)

最新更新