我正在尝试向任何网址发送http请求,并使用urllib库获取响应。以下是我使用过的代码:
>>> import requests
>>> r = requests.get("http://www.youtube.com/results?bad+blood")
>>> r.status_code
200
当我尝试这样做时,我得到以下错误。
>>> r.json()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/Library/Python/2.7/site-packages/requests/models.py", line 808, in json
return complexjson.loads(self.text, **kwargs)
File "/Library/Python/2.7/site-packages/simplejson/__init__.py", line 516, in loads
return _default_decoder.decode(s)
File "/Library/Python/2.7/site-packages/simplejson/decoder.py", line 370, in decode
obj, end = self.raw_decode(s)
File "/Library/Python/2.7/site-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 1 column 3 (char 2)
有人可以告诉我代码有什么不对。
PS:我正在使用python 2.7.10
答案 0 :(得分:2)
响应不是JSON,而是'text/html; charset=utf-8'
。如果要解析它,请使用BeautifulSoup之类的内容。
>>> import requests, bs4
>>> rsp = requests.get('http://www.youtube.com/results?bad+blood')
>>> rsp.headers['Content-Type']
'text/html; charset=utf-8'
>>> soup = bs4.BeautifulSoup(rsp.content, 'html.parser')
我建议您改用YouTube Search API。登录Google Developers Console,在API Key Setup instructions后设置API密钥,然后您可以使用YouTube Search API发出请求:
>>> from urllib import parse
>>> import requests
>>> query = parse.urlencode({'q': 'bad blood',
... 'part': 'snippet',
... 'key': 'OKdE7HRNPP_CzHiuuv8FqkaJhPI2MlO8Nns9vuM'})
>>> url = parse.urlunsplit(('https', 'www.googleapis.com',
... '/youtube/v3/search', query, None))
>>> rsp = requests.get(url, headers={'Accept': 'application/json'})
>>> rsp.raise_for_status()
>>> response = rsp.json()
>>> response.keys()
dict_keys(['pageInfo', 'nextPageToken', 'regionCode', 'etag', 'items', 'kind'])
请注意,该示例使用的是Python 3.如果要使用Python 2,则必须从urlencode
导入urllib
,从urlunsplit
导入urlparse
。
答案 1 :(得分:1)
该网址返回的是HTML,而非JSON,因此无需在响应中调用ec2
。