import json
import requests
url = "https://www.ted.com/talks/emily_balcetis_why_some_people_find_exercise_harder_than_others/transcript.json?language=en"
data = requests.get(url).json()
实际上,我正在解析100多个URL,但是有时会遇到错误,而有时却没有。 例如,此URL 1小时前并未引发此错误,而现在正在引发。
Traceback (most recent call last):
File "tedcrawler.py", line 89, in <module>
EnglishTranscript(url)
File "tedcrawler.py", line 58, in EnglishTranscript
data = requests.get(url).json()
File "/usr/local/lib/python2.7/dist-packages/requests/models.py", line 896, in json
return complexjson.loads(self.text, **kwargs)
File "/usr/lib/python2.7/dist-packages/simplejson/__init__.py", line 516, in loads
return _default_decoder.decode(s)
File "/usr/lib/python2.7/dist-packages/simplejson/decoder.py", line 370, in decode
obj, end = self.raw_decode(s)
File "/usr/lib/python2.7/dist-packages/simplejson/decoder.py", line 400, in raw_decode
return self.scan_once(s, idx=_w(s, idx).end())
simplejson.scanner.JSONDecodeError: Expecting value: line 2 column 1 (char 1)
已解决:错误是由于请求数过多(状态码为429
)答案 0 :(得分:1)
可能您收到速率限制或其他一些服务器端错误。您可以尝试检查状态代码是否为status_ok
,然后解析json。
import json
import requests
url = "https://www.ted.com/talks/emily_balcetis_why_some_people_find_exercise_harder_than_others/transcript.json?language=hi"
data = requests.get(url)
print data.status_code
if data.status_code == 200:
print data.json()