0

嘿,我刚开始使用 Python 3.8,最后一行出现错误。它在 python 3.6 上完美运行我如何让它在 Python 3.6 上运行?

在 raw_decode 从 None json.decoder.JSONDecodeError 中引发 JSONDecodeError("Expecting value", s, err.value): Expecting value: line 1 column 1 (char 0)

import requests
import urllib.request, urllib.parse
from bs4 import BeautifulSoup
import json
user = "travel.like.this"
url = 'https://www.instagram.com/' + user
req = urllib.request.Request(url)
req.add_header('User-Agent','Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.110 Safari/537.36')
html = urllib.request.urlopen(req).read()
response = BeautifulSoup(html, 'html.parser')
jsonObject = response.select("body > script:nth-of-type(1)")[0].text.replace('window._sharedData =','').replace(';','')
data = json.loads(jsonObject)
4

1 回答 1

0

也许在python 3.8requestslibnot handling the empty response和它的编码在它为空时引发错误。

这个错误:JSONDecodeError: Expecting value: line 1 column 1 (char 0)

是因为 json 响应是empty,它是一个void字符串。---->b''

(我没有看到 json,但我认为它是空的,这就是它引发错误的原因)

根据我的经验:

import requests

response = requests.get("https://my_static_website.com/")
print(response.status_code) # 200
print(response.json())

出去

200

JSONDecodeError: Expecting value: line 1 column 1 (char 0)
于 2021-03-03T18:22:02.720 回答