python - 将 MLB 网站上的游戏统计表读入美丽的汤
问题描述
我正在尝试从 MLB 球员网站(https://www.mlb.com/player/charlie-morton-450203?stats=gamelogs-r-pitching-mlb&year=2019)中抓取/读取 Game Stats 表。我似乎无法找到/捕获类名。当我在 chrome 中“检查 HTML”时,我可以看到类名,但美丽的汤似乎找不到。
是否有一些解决方法/技巧可以正确输入?
from bs4 import BeautifulSoup
import requests
page = requests.get('https://www.mlb.com/player/charlie-morton-450203?stats=gamelogs-r-pitching-mlb&year=2019')
soup = BeautifulSoup(page.text, "html.parser")
body = soup.find('body')
table = body.findAll('div', {'class':'gamelogs-table'})
print(table)
解决方案
数据通过 AJAX 加载。对于正确的数据源,您需要通过例如 Firefox 中的开发者控制台找到 URL。此脚本打印 player 的 JSON 数据450203
:
import requests
import json
url = 'https://statsapi.mlb.com/api/v1/people/450203/stats?stats=gameLog'
data = requests.get(url).json()
print(json.dumps(data, indent=4))
推荐阅读
- ruby-on-rails - How to save into another table when one table's one record changed from nil to not nil with Rails?
- javascript - 我可以在操作之前将元素上的事件恢复为状态吗?
- python - django后台,文字太长不好看,如何缩小文字
- javascript - 想要在单个页面上有多个谷歌方向图
- node.js - mongoose 中单个控制器上的多个模型值
- docker - 运行“docker-compose up”时出现错误“错误:yaml.scanner.ScannerError:此处不允许映射值”
- c# - 在 .NET Core 中将 HubContext 设为 null
- applescript - 日期选择器显示问题(主线程问题?)
- c - 如何限制用户输入非数字字符?
- reactjs - 设置 paddingTop: Constants.statusBarHeight trought StatusBar 组件,没有expo