如何从flashscore中抓取与比赛详细信息对应的所有超链接?

问题描述 投票:0回答:2

我想从 flashscore 页面抓取匹配项的超链接。

例如,如果我访问 https://www.flashscore.com/football/france/ligue-1-2021-2022/results/ 我有比赛和结果列表,我想获取所有比赛详细信息的链接在此页面中,因此要获取链接:https://www.flashscore.com/match/K2ED4alK/#/match-summary/match-summary,然后下一个链接是https://www.flashscore .com/match/EPDH3J3Q/#/match-summary/match-summary .

我尝试使用这样的代码获取这些数据:

import requests
from bs4 import BeautifulSoup
url = 'https://www.flashscore.com/football/france/ligue-1-2022-2023/results/'
reqs = requests.get(url)
soup = BeautifulSoup(reqs.text, 'html.parser')
 
urls = []
for link in soup.find_all('a'):
    print(link.get('href'))

但它仅适用于顶部和左侧的栏,但我无法获得匹配的超链接。我怎样才能得到它们?

python web-scraping beautifulsoup python-requests
2个回答
0
投票

先获取div的id,然后即可获取url,如下:


0
投票

这里的主要问题是内容是动态加载/渲染的,无法通过

requests
直接处理,因为它不是浏览器,只能处理静态响应。

您可以尝试通过 API 和请求获取匹配项,或者使用

selenium
来模仿浏览器行为。

第二种方式可以在不同的扩张阶段实施。您可以点击所有摘要链接并进一步导航,也可以收集链接然后单独调用它们,最终取决于您想要实现的目标。

为了完成@LetsScrapeData的方法,由于上述情况,它不能纯粹与

requests
一起工作,这里是一个收集链接的示例:

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

driver = webdriver.Chrome()
driver.maximize_window()

url = f'https://www.flashscore.com/football/france/ligue-1-2022-2023/results/'
driver.get(url)

link_list = [f'https://www.flashscore.com/match/{id.get_attribute("id").strip("g_1_")}/#/match-summary/match-summary' for id in WebDriverWait(driver, 30).until(EC.presence_of_all_elements_located((By.CSS_SELECTOR, '[id^=g_1_]')))]

link_list

['https://www.flashscore.com/match/W0ldfU30/#/match-summary/match-summary',
 'https://www.flashscore.com/match/p4h0glJ6/#/match-summary/match-summary',
 'https://www.flashscore.com/match/W0AR993m/#/match-summary/match-summary',
 'https://www.flashscore.com/match/6Jt9iSmJ/#/match-summary/match-summary',
 'https://www.flashscore.com/match/SSs5h8YC/#/match-summary/match-summary',
 'https://www.flashscore.com/match/YPUjGBQJ/#/match-summary/match-summary',
 'https://www.flashscore.com/match/CGTfFVuQ/#/match-summary/match-summary',
 'https://www.flashscore.com/match/Ic6NAkls/#/match-summary/match-summary',
 'https://www.flashscore.com/match/QN9V8TI/#/match-summary/match-summary',...]
© www.soinside.com 2019 - 2024. All rights reserved.