Python:JSONDecodeError错误(json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0))原因记

缘起:最近学习Python,用到json模块,常出现JSONDecodeError错误,怕自己忘记,所以记录下来,目前发现一个原因和改正方法:
错误代码(获取饿了么网站输入地址附近的饭店信息):

import requests,json
url = 'https://www.ele.me/place/wqj*y*vgc**s?latitude=**.243668&longitude=**.915411'
file = requests.get(url)
res = json.loads(file.text)
file1 = open('饭店.txt','a',encoding = 'utf-8')
for x in res:
    print(x['name'])
    file1.write(x['name']+'\n')
file1.close()

运行时报错:

Traceback (most recent call last):
File “D:/Python-EX/t3.py”, line 4, in
res = json.loads(file.text)
File “C:\Users\imach\AppData\Local\Programs\Python\Python37-32\lib\json_init_.py”, line 348, in loads
return _default_decoder.decode(s)
File “C:\Users\imach\AppData\Local\Programs\Python\Python37-32\lib\json\decoder.py”, line 337, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File “C:\Users\imach\AppData\Local\Programs\Python\Python37-32\lib\json\decoder.py”, line 355, in raw_decode
raise JSONDecodeError(“Expecting value”, s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

错误提示是在运行第四行代码中的json时出错。
错误原因是代码中输入的网址不正确,此处输入的网址是浏览器中的网址,正确的网址是:
检查源代码>>XHR>>Headers>>General>>Request URL中提供的网址。将Request URL中提供的网址输入到代码第二行(url =’‘)后可以正常运行。

你可能感兴趣的:(学习笔记)