【发布时间】: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