Python 3.4 HTTP Error 505 retrieving json from url

独自空忆成欢 提交于 2019-12-11 10:04:47

问题


I am trying to connect to a page that takes in some values and return some data in JSON format in Python 3.4 using urllib. I want to save the values returned from the json into a csv file.

This is what I tried...

import json
import urllib.request

url = 'my_link/select?wt=json&indent=true&f=value'
response = urllib.request.Request(url)    
response = urllib.request.urlopen(response)
data = response.read()

I am getting an error below:

urllib.error.HTTPError: HTTP Error 505: HTTP Version Not Supported

EDIT: Found a solution to my problem. I answered it below.


回答1:


You have found a server that apparently doesn't want to talk HTTP/1.1. You could try lying to it by claiming you are using a HTTP/1.0 client instead, by patching the http.client.HTTPConnection class:

import http.client
http.client.HTTPConnection._http_vsn = 10
http.client.HTTPConnection._http_vsn_str = 'HTTP/1.0'

and re-trying your request.




回答2:


I used FancyURLopener and it worked. Found this useful: docs.python.org: urllib.request

url_request = urllib.request.FancyURLopener({})
with url_request.open(url) as url_opener:
        json_data = url_opener.read().decode('utf-8')
        with open(file_output, 'w', encoding ='utf-8') as output:
            output.write(json_data)

Hope this helps those having the same problems as mine.



来源:https://stackoverflow.com/questions/31893207/python-3-4-http-error-505-retrieving-json-from-url

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!