JSONDecodeError:应为值:第1行第1列(字符0)在第response.json()行
我在response.json()行上获得错误值:第1行第1列(字符0) 我尝试的 与其他答案相反,我在json.loads()处没有得到错误,但在response.json()处得到错误。我也尝试过调整响应数据体,但没有任何帮助。下面是代码JSONDecodeError:应为值:第1行第1列(字符0)在第response.json()行,json,python-3.x,python-requests,Json,Python 3.x,Python Requests,我在response.json()行上获得错误值:第1行第1列(字符0) 我尝试的 与其他答案相反,我在json.loads()处没有得到错误,但在response.json()处得到错误。我也尝试过调整响应数据体,但没有任何帮助。下面是代码 import requests import json from bs4 import BeautifulSoup as Soup def get_links(search_name): search_name = search_name.rep
import requests
import json
from bs4 import BeautifulSoup as Soup
def get_links(search_name):
search_name = search_name.replace(' ', '+')
response = requests.get("http://www.google.com/search",
params={'q': search_name, 'first': 0},
headers={'User-Agent': user_agent})
dataform = response.json()
page = json.loads(dataform)
new_soup = Soup(page[1][1], 'lxml')
images = new_soup.find_all('img')
links = [image['src'] for image in images]
return links
回溯:
<ipython-input-27-8d351939888e> in get_links(search_name)
17 params={'q': search_name, 'first': 0},
18 headers={'User-Agent': user_agent})
---> 19 dataform = response.json()
20 page = json.loads(dataform)
21 new_soup = Soup(page[1][1], 'lxml')
/usr/local/lib/python3.6/dist-packages/requests/models.py in json(self, **kwargs)
890 # used.
891 pass
--> 892 return complexjson.loads(self.text, **kwargs)
893
894 @property
/usr/lib/python3.6/json/__init__.py in loads(s, encoding, cls, object_hook, parse_float, parse_int, parse_constant, object_pairs_hook, **kw)
352 parse_int is None and parse_float is None and
353 parse_constant is None and object_pairs_hook is None and not kw):
--> 354 return _default_decoder.decode(s)
355 if cls is None:
356 cls = JSONDecoder
/usr/lib/python3.6/json/decoder.py in decode(self, s, _w)
337
338 """
--> 339 obj, end = self.raw_decode(s, idx=_w(s, 0).end())
340 end = _w(s, end).end()
341 if end != len(s):
/usr/lib/python3.6/json/decoder.py in raw_decode(self, s, idx)
355 obj, end = self.scan_once(s, idx)
356 except StopIteration as err:
--> 357 raise JSONDecodeError("Expecting value", s, err.value) from None
358 return obj, end
JSONDecodeError: Expecting value: line 1 column 1 (char 0)
获取链接中的(搜索名称)
17参数={'q':搜索名称,'first':0},
18头={'User-Agent':User\u-Agent})
--->19 dataform=response.json()
20页=json.loads(数据格式)
21新汤=汤(第[1][1]页,“lxml”)
/json中的usr/local/lib/python3.6/dist-packages/requests/models.py(self,**kwargs)
890已使用。
891通行证
-->892返回complexjson.load(self.text,**kwargs)
893
894@property
/加载中的usr/lib/python3.6/json/\uuuuu init\uuuuuuu.py(s、编码、cls、对象钩子、解析浮点数、解析整型、解析常数、对象对钩子,**kw)
352 parse_int为无,parse_float为无,且
353 parse_常量为None且对象_pairs_hook为None且非kw):
-->354返回\u默认\u解码器。解码
355如果cls为无:
356 cls=JSONDecoder
/解码中的usr/lib/python3.6/json/decoder.py(self,s,_w)
337
338 """
-->339 obj,end=self.raw\u decode(s,idx=\u w(s,0.end())
340 end=_w(s,end).end()
341如果结束!=长度:
/原始解码中的usr/lib/python3.6/json/decoder.py(self、s、idx)
355 obj,end=自扫描一次(s,idx)
356除了停止迭代作为错误:
-->357将JSONDecodeError(“预期值”,s,err.value)从None提升
358返回obj,结束
JSONDecodeError:应为值:第1行第1列(字符0)
函数response.json()
尝试解码作为请求响应发送的任何json。但显然,它不会返回json;而是用html响应
这可以从response.text
中看出,它将返回的html显示为字符串
例如,如果您使用一个响应实际JSON的url,那么您将不会得到任何错误