排查解决 json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

python爬虫起步时碰到该错误,如下代码:
(python版本,3.7)

url = https://www.douban.com/j/search_photo?q=熊猫&limit=20&start=0
html = requests.get(url).text 
response = json.loads(html)

运行时报错:
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

开始debug

  • 1 首先验证 html 是否为 json
print(html)

结果为空,那么可以加判断,避免报错

if html :
	response = json.loads(html)

然而 此处接口直接访问是有数据的,不只要避免报错,还要拿到数据

  • 2 requests get结果的code码
html = requests.get(url)
print(html.status_code)

status_code 为418,正常场景下的code码应为200

  • 3 解决requests.get() status_code 418的问题
headers = {'User-Agent':'Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36'}
url = https://www.douban.com/j/search_photo?q=熊猫&limit=20&start=0
html = requests.get(url, headers=headers)
print(html.status_code)

此时请求正常,可以拿到回参

  • 4 相对优雅的处理 —— 捕获异常
try:
	response = json.loads(html.text)
except Exception as ex:
	print(ex)

异常捕获打印后,更便于debug

至此,json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0) 问题解决

你可能感兴趣的:(数据)