我想從我最喜歡的俱樂部阿森納刮掉每一場足球比賽。
我打算抓取的網站:https ://fbref.com 。
我想刮掉每一個在每場比賽中得分的球員。
這是我的問題:如果阿森納沒有進球,但例如只收到了一名球員的紅牌,那么我不想刮掉球員的名字。
我想包含的 div 示例:
<div><div class="event\\\_icon goal"></div> <a href="/en/players/d5dd5f1f/Pierre-Emerick-Aubameyang">Pierre-Emerick Aubameyang</a> · 17’</div>
我要排除的 div 示例:
<div><div class="event\\\_icon red\\\_card"></div> <a href="/en/players/e61b8aee/Granit-Xhaka">Granit Xhaka</a> · 35’</div> </div>
我的問題:如何排除與紅牌相關的 div/玩家?
比賽鏈接示例 = https://fbref.com/en/matches/d4650aa2/Manchester-City-Arsenal-August-28-2021-Premier-League。
我的代碼
import re
match_data_list = []
for index, team in enumerate(team_list):
link, opponent, venue = team
#Because of devlopement i only iterate 4 times
if index <= 4:
if venue == "Home":
data_team_stat = requests.get(link)
soup_team = BeautifulSoup(data_team_stat.text)
match_data = soup_team.select('div.scorebox div#a')
for div in match_data:
print(div)
for player in div:
player_name = re.sub("[^A-Za-z*é?\s-]","",player.text)
player_name = player_name.rstrip().strip()
if player_name != "":
data = (player_name, opponent)
match_data_list.append(data)
else:
data_team_stat = requests.get(link)
soup_team = BeautifulSoup(data_team_stat.text)
match_data = soup_team.select('div.scorebox div#b')
for div in match_data:
print(div)
for player in div:
player_name = re.sub("[^A-Za-z*é?\s-]","",player.text)
player_name = player_name.rstrip().strip()
if player_name != "":
data = (player_name, opponent)
match_data_list.append(data)
else:
break
uj5u.com熱心網友回復:
如果您只是想用目標抓取事件,請調整您的選擇并使其更具體:
soup.select('.scorebox #b div:has(.goal)')
用于css selectors僅在具有類<div>的元素中選擇得分框。id=bgoal
或者回答您關于排除使用pseudo-classes組合的問題,例如:has(:not()):
soup.select('.scorebox #b div:not(:has(.red_card))')
例子
from bs4 import BeautifulSoup
import requests
url = 'https://fbref.com/en/matches/d4650aa2/Manchester-City-Arsenal-August-28-2021-Premier-League'
page=requests.get(url).content
soup=BeautifulSoup(page)
goals_a = [p.a.text for p in soup.select('.scorebox #a div:has(.goal)')]
goals_b = [p.a.text for p in soup.select('.scorebox #b div:has(.goal)')]
或明確排除:
goals_a = [p.a.text for p in soup.select('.scorebox #a div:has(:not(.red_card))') if p.a]
goals_b = [p.a.text for p in soup.select('.scorebox #b div:not(:has(.red_card))') if p.a]
輸出
['?lkay Gündo?an', 'Ferrán Torres', 'Gabriel Jesus', 'Rodri', 'Ferrán Torres']
[]
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/490653.html
上一篇:SeleniumWebdriver找不到加載晚于DOM的元素
下一篇:動態類名的Apify選擇器
