Cryssie
Cryssie

Reputation: 3175

Python 3.4 HTTP Error 505 retrieving json from url

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.

Upvotes: 1

Views: 1600

Answers (2)

Cryssie
Cryssie

Reputation: 3175

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.

Upvotes: 0

Martijn Pieters
Martijn Pieters

Reputation: 1121914

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.

Upvotes: 1

Related Questions