jupyter notebook 上写如下代码,报错
import requests
import json
res = requests.get('http://cre.dp.sina.cn/api/v3/get?cre=newsw&mod=f&merge=3&statics=1&cateid=1o&offset=0&length=100&_=1481721737306&callback=jsonp1')
jd = json.loads(res.text.lstrip(' jsonp1(').rstrip(');'))
jd 报错如下
JSONDecodeError Traceback (most recent call last)
<ipython-input-27-41139b3d1270> in <module>()
2 import json
3 res = requests.get('http://cre.dp.sina.cn/api/v3/get?cre=newsw&mod=f&merge=3&statics=1&cateid=1o&offset=0&length=100&_=1481721737306&callback=jsonp1')
----> 4 jd = json.loads(res.text.lstrip(' jsonp1(').rstrip(');'))
5 jd
c:\users\administrator\appdata\local\programs\python\python35-32\lib\json\__init__.py in loads(s, encoding, cls, object_hook, parse_float, parse_int, parse_constant, object_pairs_hook, **kw)
317 parse_int is None and parse_float is None and
318 parse_constant is None and object_pairs_hook is None and not kw):
--> 319 return _default_decoder.decode(s)
320 if cls is None:
321 cls = JSONDecoder
c:\users\administrator\appdata\local\programs\python\python35-32\lib\json\decoder.py in decode(self, s, _w)
340 end = _w(s, end).end()
341 if end != len(s):
--> 342 raise JSONDecodeError("Extra data", s, end)
343 return obj
344
JSONDecodeError: Extra data: line 2 column 143216 (char 143216)
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。
jsonp是JSON格式加上前缀后缀变成兼容的可执行的Javascript,那么自然去掉这个前缀和后缀就好
<preclass="brush:python;toolbar:true;auto-links:false;">_jsonp_begin=u'callback('_jsonp_end=u')'importjsondeffrom_jsonp(jsonp_str):jsonp_str=jsonp_str.strip()ifnotjsonp_str.startswith(_jsonp_begin)or\notjsonp_str.endswith(_jsonp_end):raiseValueError('InvalidJSONP')returnjson.loads(jsonp_str[len(_jsonp_begin):-len(jsonp_end)])解析JSONP应该只接受固定的格式,因为在网页当中JSONP也是这么使用的,回调函数必须跟预先设定的一致。最多也就最后有没有分号的差别。如果出现了预想以外的格式,都应该认为是错误。
https://www.zhihu.com/question/52841349
Notsureaboutthereasonbutithasadditionalcharsintheend.