【问题标题】:JSONDecodeError: Expecting value: line 1 column 1 (char 0) - Not working on Python 3.8JSONDecodeError:期望值:第 1 行第 1 列(字符 0)- 不适用于 Python 3.8
【发布时间】:2020-08-03 15:54:16
【问题描述】:

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

在 raw_decode 中引发 JSONDecodeError("Expecting value", s, err.value) from None json.decoder.JSONDecodeError:预期值:第 1 行第 1 列(字符 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)

【问题讨论】:

  • jsonObject 的值是多少?因为它看起来不是有效的 JSON。
  • 应该是html请求的字符串。我进一步检查,似乎有一行适用于 python 3.6,而在 python 3.8 上它什么也不返回: jsonObject = response.select("body > script:nth-of-type(1)")[0].text.replace( 'window._sharedData =','').replace(';','')

标签: python json list python-requests python-responses


【解决方案1】:

可能在python 3.8 中,requests 库是not 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)

【讨论】:

    猜你喜欢
    • 2013-05-10
    • 2019-02-25
    • 2018-06-28
    • 2018-08-28
    • 2020-10-10
    • 1970-01-01
    • 2021-12-05
    • 1970-01-01
    相关资源
    最近更新 更多