python 使用find_all,代码只查找第二个href,而不查找第一个href

xzabzqsa  于 2023-03-11  发布在  Python
关注(0)|答案(1)|浏览(126)

我想把transfermarkt最近的转会历史(第二页)的第二页刮下来。但是,我只找到了“Aufnehmender Verein”的hrefs,但我需要两个hrefs。
我的代码:

url = "https://www.transfermarkt.com/transfers/saisontransfers/statistik?ajax=yw0&altersklasse=&ausrichtung=&land_id=0&leihe=&page=2plus=2&saison-id=2022&spielerposition_id=&transferfenster=wintertransfers"
headers = {"User-Agent": "Mozilla/5.0 (Windows NT 10.0; WOW64; rv:40.0) Gecko/20100101 Firefox/40.0"}
tree = requests.get(url, headers = headers)
print(tree.status_code)

soup = BeautifulSoup(tree.content, 'html.parser')

for tra in table.find_all('a', href=re.compile('startseite/verein')):
        #teams.append(tra.text)      
    liga.append(tra.find_next('td').text)
joining_team = liga[0::2]

但是我也需要离开的队伍!
能帮我一下吗?

2g32fytz

2g32fytz1#

要获得球员的详细信息(从他们去哪里等),您必须使用不同的 AJAX URL:

import pandas as pd
import requests
from bs4 import BeautifulSoup

url = 'https://www.transfermarkt.com/transfers/saisontransfers/statistik?land_id=0&ausrichtung=&spielerposition_id=&altersklasse=&leihe=&transferfenster=wintertransfers&saison-id=0&plus=1&page={page}'

headers = {'User-Agent': 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:109.0) Gecko/20100101 Firefox/110.0'}

data = []
for page in range(1, 3):  # <-- increase number of pages here
    soup = BeautifulSoup(requests.get(url.format(page=page), headers=headers).content, 'html.parser')
    for row in soup.select('table.items tbody > tr'):
        tds = [td.get_text(strip=True, separator=', ') for td in row.find_all('td', recursive=False)]
        data.append(tds)

df = pd.DataFrame(data, columns=['#', 'Player', 'Age', 'Market value', 'Nat.', 'Left', 'Joined', 'Fee'])
print(df)

图纸:

#                                   Player Age Market value Nat.                                     Left                                     Joined                 Fee
0    1         Enzo Fernández, Central Midfield  22      €55.00m                        Benfica, Liga Portugal                    Chelsea, Premier League            €121.00m
1    2             Mykhaylo Mudryk, Left Winger  22      €40.00m                     Shakhtar D., Premier Liga                    Chelsea, Premier League             €70.00m
2    3                 João Cancelo, Right-Back  28      €70.00m                      Man City, Premier League                  Bayern Munich, Bundesliga       loan transfer
3    4                  Cody Gakpo, Left Winger  23      €60.00m                     PSV Eindhoven, Eredivisie                  Liverpool, Premier League             €42.00m
4    5               João Félix, Second Striker  23      €50.00m                       Atlético Madrid, LaLiga                    Chelsea, Premier League  Loan fee:, €11.00m
5    6              Anthony Gordon, Left Winger  21      €40.00m                       Everton, Premier League                  Newcastle, Premier League             €45.60m
6    7           Benoît Badiashile, Centre-Back  21      €40.00m                               Monaco, Ligue 1                    Chelsea, Premier League             €38.00m
7    8               Noni Madueke, Right Winger  20      €15.00m                     PSV Eindhoven, Eredivisie                    Chelsea, Premier League             €35.00m
8    9             Jorginho, Defensive Midfield  31      €35.00m                       Chelsea, Premier League                    Arsenal, Premier League             €11.30m
9   10              Arnaut Danjuma, Left Winger  25      €35.00m                            Villarreal, LaLiga                  Tottenham, Premier League   Loan fee:, €3.00m
10  11                  Vitinha, Centre-Forward  22      €15.00m                       SC Braga, Liga Portugal                         Marseille, Ligue 1             €32.00m
11  12           Gonçalo Guedes, Second Striker  26      €32.00m                        Wolves, Premier League                     Benfica, Liga Portugal       loan transfer
12  13                   Malo Gusto, Right-Back  19      €15.00m                       Olympique Lyon, Ligue 1                    Chelsea, Premier League             €30.00m
13  14            Leandro Trossard, Left Winger  28      €30.00m                      Brighton, Premier League                    Arsenal, Premier League             €24.00m
14  15       Nicolò Zaniolo, Attacking Midfield  23      €30.00m                              AS Roma, Serie A                     Galatasaray, Süper Lig             €15.00m

...and so on.

相关问题