我在IMDb上发出请求,要求将电影信息提取到JSON中。这是我的代码:
import json
import requests
url = "http://www.imdb.com/title/tt3385516/"
re = requests.get(url).json()
它给我一个错误,我不知道如何处理它:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/local/lib/python3.5/dist-packages/requests/models.py", line 799, in json
return json.loads(self.text, **kwargs)
File "/usr/lib/python3.5/json/__init__.py", line 319, in loads
return _default_decoder.decode(s)
File "/usr/lib/python3.5/json/decoder.py", line 339, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "/usr/lib/python3.5/json/decoder.py", line 357, in raw_decode
raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 8 column 1 (char 7)
我尝试使用
re = requests.get(url)
data = json.loads(re.text)
但它让我又犯了一个错误:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python3.5/json/__init__.py", line 319, in loads
return _default_decoder.decode(s)
File "/usr/lib/python3.5/json/decoder.py", line 339, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "/usr/lib/python3.5/json/decoder.py", line 357, in raw_decode
raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 8 column 1 (char 7)
答案 0 :(得分:2)
响应是HTML,而不是JSON,因此无法进行JSON解码:
>>> r = requests.get('http://www.imdb.com/title/tt3385516/')
>>> r.headers['content-type']
'text/html;charset=UTF-8'