首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >ESPN Gamecast Python Webscraping

ESPN Gamecast Python Webscraping
EN

Stack Overflow用户
提问于 2021-06-24 13:09:07
回答 2查看 208关注 0票数 0

我有困难从ESPN记分板网页抓取espn Gamecast链接。我试过:

代码语言:javascript
复制
site = "https://www.espn.com/mlb/scoreboard"

html = requests.get(site).text

soup = BeautifulSoup(html, 'html.parser').find_all('a')

links = [link.get('href') for link in soup]

但这些联系并没有被识别出来。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2021-06-30 08:42:33

它是动态加载的,因此您需要( a)使用Selenium之类的东西(允许页面在使用bs4进行解析之前呈现),或者( b)直接使用数据源/api。Api通常是最好的选择:

代码语言:javascript
复制
import requests

api = 'http://site.api.espn.com/apis/site/v2/sports/baseball/mlb/scoreboard'

jsonData = requests.get(api).json()
events = jsonData['events']

links = []
for event in events:
    event_links = event['links']
    for each in event_links:
        if each['text'] == 'Gamecast':
            links.append(each['href'])

输出:

代码语言:javascript
复制
print(links)
['http://www.espn.com/mlb/game/_/gameId/401228229', 'http://www.espn.com/mlb/game/_/gameId/401228235', 'http://www.espn.com/mlb/game/_/gameId/401228242', 'http://www.espn.com/mlb/game/_/gameId/401228240', 'http://www.espn.com/mlb/game/_/gameId/401228233', 'http://www.espn.com/mlb/game/_/gameId/401228234', 'http://www.espn.com/mlb/game/_/gameId/401228239', 'http://www.espn.com/mlb/game/_/gameId/401228237', 'http://www.espn.com/mlb/game/_/gameId/401228231', 'http://www.espn.com/mlb/game/_/gameId/401228232', 'http://www.espn.com/mlb/game/_/gameId/401228236', 'http://www.espn.com/mlb/game/_/gameId/401228230', 'http://www.espn.com/mlb/game/_/gameId/401228238', 'http://www.espn.com/mlb/game/_/gameId/401228243', 'http://www.espn.com/mlb/game/_/gameId/401228241']
票数 0
EN

Stack Overflow用户

发布于 2021-06-24 13:24:23

会不会是你漏掉了引号?我已经尝试了以下几种方法,并能产生输出。

代码语言:javascript
复制
site = 'https://www.espn.com/mlb/scoreboard/_/date/20210624'
html = requests.get(site).text
soup = BeautifulSoup(html, 'html.parser').find_all('a')
links = [link.get('href') for link in soup]
print(links)
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/68116431

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档