无法从网页中找到所有href链接

uxh89sit  于 2021-08-20  发布在  Java
关注(0)|答案(1)|浏览(441)

我正在努力获得之前在索法斯科球场为任何球队打过的比赛的href,让我们以利物浦为例(https://www.sofascore.com/team/football/liverpool/44). 我想获得他们之前5款游戏的href,但其中一款游戏是通过以下代码显示的:

import requests
from bs4 import BeautifulSoup, SoupStrainer

link = "https://www.sofascore.com/team/football/liverpool/44"

res = requests.get(link)
soup = BeautifulSoup(res.content, 'html.parser')
soup = soup.find('main')

for link in BeautifulSoup(res.content, "html.parser", parse_only=SoupStrainer('a', href=True)):
        print(link['href'])

一些href正在显示,但不是我需要的。

0yycz8jy

0yycz8jy1#

您看到的数据是通过ajax从外部源加载的。您可以使用 requests 模块:

import json
import requests

# '44' is the ID from your URL:

api_url = "https://api.sofascore.com/api/v1/team/44/events/last/0"
data = requests.get(api_url).json()

# uncomment this to print all data

# print(json.dumps(data, indent=4))

for e in data["events"]:
    if e["status"]["code"] != 100:
        continue

    event_url = "https://www.sofascore.com/" + e["slug"] + "/" + e["customId"]
    print(
        "{:>25} - {:<25} {:<2}:{:>2}  {:<70}".format(
            e["homeTeam"]["name"],
            e["awayTeam"]["name"],
            e["homeScore"]["current"],
            e["awayScore"]["current"],
            event_url,
        )
    )

印刷品:

Newcastle United - Liverpool                 0 : 0  https://www.sofascore.com/liverpool-newcastle-united/OU               
              Southampton - Liverpool                 1 : 0  https://www.sofascore.com/southampton-liverpool/UsV                   
              Aston Villa - Liverpool                 1 : 4  https://www.sofascore.com/liverpool-aston-villa/PU                    
                Liverpool - Manchester United         0 : 0  https://www.sofascore.com/liverpool-manchester-united/KU              
                Liverpool - Burnley                   0 : 1  https://www.sofascore.com/burnley-liverpool/gsU                       
        Manchester United - Liverpool                 3 : 2  https://www.sofascore.com/liverpool-manchester-united/KU              
                Tottenham - Liverpool                 1 : 3  https://www.sofascore.com/liverpool-tottenham/IsU                     
          West Ham United - Liverpool                 1 : 3  https://www.sofascore.com/liverpool-west-ham-united/MU                
                Liverpool - Brighton & Hove Albion    0 : 1  https://www.sofascore.com/liverpool-brighton-and-hove-albion/FsU      
                Liverpool - Manchester City           1 : 4  https://www.sofascore.com/liverpool-manchester-city/rU                
           Leicester City - Liverpool                 3 : 1  https://www.sofascore.com/liverpool-leicester-city/GU                 
               RB Leipzig - Liverpool                 0 : 2  https://www.sofascore.com/liverpool-rb-leipzig/UskCo                  
                Liverpool - Everton                   0 : 2  https://www.sofascore.com/everton-liverpool/UsY                       
         Sheffield United - Liverpool                 0 : 2  https://www.sofascore.com/liverpool-sheffield-united/psU              
                Liverpool - Chelsea                   0 : 1  https://www.sofascore.com/liverpool-chelsea/NsU                       
                Liverpool - Fulham                    0 : 1  https://www.sofascore.com/liverpool-fulham/TsU                        
                Liverpool - RB Leipzig                2 : 0  https://www.sofascore.com/liverpool-rb-leipzig/UskCo                  
            Wolverhampton - Liverpool                 0 : 1  https://www.sofascore.com/liverpool-wolverhampton/dsU                 
                  Arsenal - Liverpool                 0 : 3  https://www.sofascore.com/liverpool-arsenal/HQfh                      
              Real Madrid - Liverpool                 3 : 1  https://www.sofascore.com/liverpool-real-madrid/UsEgb                 
                Liverpool - Aston Villa               2 : 1  https://www.sofascore.com/liverpool-aston-villa/PU                    
                Liverpool - Real Madrid               0 : 0  https://www.sofascore.com/liverpool-real-madrid/UsEgb                 
             Leeds United - Liverpool                 1 : 1  https://www.sofascore.com/liverpool-leeds-united/JsU                  
                Liverpool - Newcastle United          1 : 1  https://www.sofascore.com/liverpool-newcastle-united/OU               
                Liverpool - Southampton               2 : 0  https://www.sofascore.com/southampton-liverpool/UsV                   
        Manchester United - Liverpool                 2 : 4  https://www.sofascore.com/liverpool-manchester-united/KU              
     West Bromwich Albion - Liverpool                 1 : 2  https://www.sofascore.com/west-bromwich-albion-liverpool/iU           
                  Burnley - Liverpool                 0 : 3  https://www.sofascore.com/burnley-liverpool/gsU                       
                Liverpool - Crystal Palace            2 : 0  https://www.sofascore.com/crystal-palace-liverpool/hsU

相关问题