Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 从url解析时,Urllib请求引发解码错误_Python_Json_Decode_Urllib - Fatal编程技术网

Python 从url解析时,Urllib请求引发解码错误

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

我正在尝试从以下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 = 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,这非常好用。