https://www.transfermarkt.us/manchester-city/kader/verein/281/saison_id/2020/plus/1
从上面的网站,我正在尝试使用Beautifulsoup解析号码,玩家名称和位置。
例如,我要打印
我尝试了类似的东西
page = requests.get(url, headers={'User-Agent':'Mozilla/5.0'})
soup = bs(page.content, 'html.parser')
rows = soup.find("table", class_="items").find('tbody').find_all('a')
for row in rows:
if row.find('img') is None:
continue
print(row.find('img')['title'])
print('\n')
首先打印名称,但不一定表示玩家名称,有时值是空的。同样,在该分支中获取数字和位置数据似乎是不可能的。如何同时访问其他分支机构以获取编号和位置数据?
要获取玩家编号,姓名和位置,可以使用以下示例:
import requests
from bs4 import BeautifulSoup
url = 'https://www.transfermarkt.us/manchester-city/kader/verein/281/saison_id/2020/plus/1'
headers = {'User-Agent': 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:80.0) Gecko/20100101 Firefox/80.0'}
soup = BeautifulSoup(requests.get(url, headers=headers).content, 'html.parser')
for row in soup.select('table.items > tbody > tr:has(td)'):
data = row.get_text(strip=True, separator='|').split('|')
print('{:<5} {:<30} {}'.format(data[0], data[1], data[3] if len(data) == 10 else data[4]))
印刷品:
31 Ederson Goalkeeper
- Zack Steffen Goalkeeper
49 Arijanet Muric Goalkeeper
33 Scott Carson Goalkeeper
14 Aymeric Laporte Centre-Back
5 John Stones Centre-Back
6 Nathan Aké Centre-Back
50 Eric García Centre-Back
30 Nicolás Otamendi Centre-Back
25 Fernandinho Centre-Back
34 Philippe Sandler Centre-Back
24 Tosin Adarabioyo Centre-Back
78 Taylor Harwood-Bellis Centre-Back
22 Benjamin Mendy Left-Back
11 Oleksandr Zinchenko Left-Back
12 Angeliño Left-Back
2 Kyle Walker Right-Back
27 João Cancelo Right-Back
- Yan Couto Right-Back
16 Rodri Defensive Midfield
8 Ilkay Gündogan Central Midfield
47 Phil Foden Central Midfield
17 Kevin De Bruyne Attacking Midfield
- Luka Ilic Attacking Midfield
7 Raheem Sterling Left Winger
- Marlos Moreno Left Winger
20 Bernardo Silva Right Winger
26 Riyad Mahrez Right Winger
21 Ferran Torres Right Winger
- Patrick Roberts Right Winger
9 Gabriel Jesus Centre-Forward
10 Sergio Agüero Centre-Forward
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句