gpt4 book ai didi

python - 无法从请求打印 JSON

转载 作者:行者123 更新时间:2023-12-01 02:47:52 25 4
gpt4 key购买 nike

我正在尝试从该网站提取 JSON,但它没有打印数据。我不确定是代码失败还是网站失败。这是代码:

import requests

season = '2016-17'
player_id = 202322
base_url = "http://stats.nba.com/stats/shotchartdetail?CFID=33&CFPARAMS=%s&ContextFilter=&ContextMeasure=FGA&DateFrom=&DateTo=&GameID=&GameSegment=&LastNGames=0&LeagueID=00&Location=&MeasureType=Base&Month=0&OpponentTeamID=0&Outcome=&PaceAdjust=N&PerMode=PerGame&Period=0&PlayerID=%s&PlusMinus=N&PlayerPosition=&Rank=N&RookieYear=&Season=%s&SeasonSegment=&SeasonType=Regular+Season&TeamID=0&VsConference=&VsDivision=&mode=Advanced&showDetails=0&showShots=1&showZones=0"
shot_chart_url = base_url % (season, player_id, season)

user_agent = 'User-Agent:Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.111 Safari/537.36'
response = requests.get(shot_chart_url, headers={'User-Agent': user_agent})

headers = response.json()['resultSets'][0]['headers']

print(headers)

最佳答案

我可以通过更改一些内容来运行您的脚本:

season = '2016-17'
player_id = 202322
base_url = "http://stats.nba.com/stats/shotchartdetail?CFID=33&CFPARAMS=%s&ContextFilter=&ContextMeasure=FGA&DateFrom=&DateTo=&GameID=&GameSegment=&LastNGames=0&LeagueID=00&Location=&MeasureType=Base&Month=0&OpponentTeamID=0&Outcome=&PaceAdjust=N&PerMode=PerGame&Period=0&PlayerID=%s&PlusMinus=N&PlayerPosition=&Rank=N&RookieYear=&Season=%s&SeasonSegment=&SeasonType=Regular+Season&TeamID=0&VsConference=&VsDivision=&mode=Advanced&showDetails=0&showShots=1&showZones=0"
shot_chart_url = base_url % (season, player_id, season)
user_agent = 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/59.0.3071.115 Safari/537.36'

headers = {
'User-Agent': user_agent,
'x-nba-stats-origin': 'stats',
'x-nba-stats-token': 'true',
'Referer': 'http://stats.nba.com/events/',
}
response = requests.get(shot_chart_url, headers=headers)
headers = response.json()['resultSets'][0]['headers']

print(headers)

我在 nba 网站内管理了一个页面,该页面使用与您相同的 api 端点,在检查了我对服务器的请求后,我执行了以下操作:

  • 添加了 Referer header - 许多服务器都需要它(这个是 ASP.NET,根据我的经验,他们确实需要它。
  • 添加了 2 个 x-nba-stats 自定义 header
  • 更改了用户代理

我认为用户代理是最重要的,我感觉他们屏蔽了ip+用户代理的组合,

编辑:只是分享我解决这个问题时的思维方式我在评论中看到这实际上适用于浏览器,知道 HTTP 如何工作这可能与:cookies/headers/url params 有关。我跳过原来的网站并搜索这个端点,它确实对我有用,我用chrome的DevTools检查http请求,并用requests模仿请求:)

关于python - 无法从请求打印 JSON,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45093866/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com