请求问题 - 无法解码JSON对象

时间:2016-03-11 19:46:56

标签: python json python-requests

我是python的新手,我试图将一些MATLAB代码转换为python。 我有一个来自NBA网站的json格式的网址,我想解析并返回数据帧。到目前为止,我有以下代码:

import requests
url = 'http://stats.nba.com/stats/shotchartdetail?PlayerID=2544&TeamID=0&VsConference=&Location=&SeasonType=Regular+Season&RookieYear=&Season=2015-16&DateFrom=&ClutchTime=&GameID=&OpponentTeamID=0&DateTo=&GameSegment=&AheadBehind=&LastNGames=0&VsDivision=&LeagueID=&Position=&Outcome=&ContextMeasure=FGM&SeasonSegment=&Period=0&Month=0'
r = requests.get(url)
data = r.json()

我可以看到json通过使用url在我的浏览器上加载,这也适用于MATLAB但我在python中收到以下错误消息: 没有JSON对象可以被解码

知道问题是什么吗?

2 个答案:

答案 0 :(得分:1)

您可以尝试获取$content = "<img type=\"asd\" style=\"color:red\" src=\"qwe\" class=\"asd\" > lorem ipsum <img type=\"asd\" style=\"color:red\" src=\"qwe\" class=\"asd\" >"; $pattern = '/style=".*?"/'; $replacement = ""; $result = preg_replace($pattern, $replacement, $content); $pattern = '/src="(.*?)"/'; $replacement = "style=\"background-image:url($1);\""; $result = preg_replace($pattern, $replacement, $result); $pattern = '/img((.*?)(style=".*?")((.*?)(>|\/>)))/'; $replacement = 'div$2$3'.'$5></div>'; echo preg_replace($pattern, $replacement, $result); 的文本元素并转换为json,如下所示:

r

虽然通过复制和粘贴代码对我有用,但这可能是一种解决方法。

答案 1 :(得分:0)

我在这里找到了答案:

Python Requests Client Error: Bad Request, but works after website has been opened in browser

添加用户代理有效:

u_a = "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/48.0.2564.82 Safari/537.36"
response = requests.get(url, headers={"USER-AGENT":u_a})