+2 votes
in Programming Languages by (5.0k points)

I am trying to download some images from a webpage using the following code.  But requests.get(ilink)) returns HTTP response code 406. The variable "ilink" is the link to the image file. How can I fix the error?

def save_image_file(ilink, filename):

    response = requests.get(ilink))

    if response.status_code == 200:

        with open(filename, 'wb') as f:

            f.write(response.content)

    else:

        print("Bad response code :", response.status_code, ilink)

1 Answer

0 votes
by (32.8k points)
edited by

You need to pass the "headers" parameter in requests.get() method. It seems that the server you are trying to access does not allow requests without a user-agent.

Modify your code as follows and it should work.

def save_image_file(ilink, filename):
    ua = 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.0.7) Gecko/2009021910 Firefox/3.0.7'
    response = requests.get(ilink, headers={"User-Agent": ua})

    if response.status_code == 200:
        with open(filename, 'wb') as f:
            f.write(response.content)
    else:
        print("Bad response code :", response.status_code, ilink)
 

If the above user-agent does not work, you can search for others and try them.

...