Python 从url解析时,Urllib请求引发解码错误
我正在尝试从以下url解析json格式的数据:。我的浏览器可以很好地处理json数据。但是请求总是抛出以下错误: JSONDecodeError:期望值:第3行第1列(字符4) 我正在使用Python 3.5。这是我的代码:Python 从url解析时,Urllib请求引发解码错误,python,json,decode,urllib,Python,Json,Decode,Urllib,我正在尝试从以下url解析json格式的数据:。我的浏览器可以很好地处理json数据。但是请求总是抛出以下错误: JSONDecodeError:期望值:第3行第1列(字符4) 我正在使用Python 3.5。这是我的代码: import json import urllib.request connection = urllib.request.urlopen('http://ws-old.parlament.ch/affairs/20080062?format=json') js = c
import json
import urllib.request
connection = urllib.request.urlopen('http://ws-old.parlament.ch/affairs/20080062?format=json')
js = connection.read()
info = json.loads(js.decode("utf-8"))
print(info)
该网站使用用户代理过滤,只向已知浏览器提供JS。幸运的是,它很容易被愚弄,只需将
User-Agent
头设置为Mozilla
:
request = urllib.request.Request(
'http://ws-old.parlament.ch/affairs/20080062?format=json',
headers={'User-Agent': 'Mozilla'})
connection = urllib.request.urlopen(request)
js = connection.read()
info = json.loads(js.decode("utf-8"))
print(info)
该网站使用用户代理过滤,只向已知浏览器提供JS。幸运的是,它很容易被愚弄,只需将
User-Agent
头设置为Mozilla
:
request = urllib.request.Request(
'http://ws-old.parlament.ch/affairs/20080062?format=json',
headers={'User-Agent': 'Mozilla'})
connection = urllib.request.urlopen(request)
js = connection.read()
info = json.loads(js.decode("utf-8"))
print(info)
urllib
不会引发此错误。它是由json.loads()
行抛出的,显然您没有收到有效的json数据。当使用Python加载时,我从该URL得到一个HTML响应。错误不是由urllib
抛出的。它是由json.loads()
行抛出的,显然您没有收到有效的json数据。当使用Python加载时,我从该URL得到了一个HTML响应。非常感谢Martjin,这非常好用。非常感谢Martjin,这非常好用。