user3752027
user3752027

Reputation: 3

Saving a randomly generated picture using Python mechanize

I want to save a picture from a web that is randomly generated at each request, I tried to get it using .retrieve() but it doesn't work (it downloads another random picture).

I wouldn't mind to download the whole page if necessary.

Upvotes: 0

Views: 103

Answers (1)

Mark Adward
Mark Adward

Reputation: 54

import urllib2
referer = 'link of the page of image'
image   = 'image link '

req = urllib2.Request(image)
req.add_header('Referer', referer)   # here is the trick
response = urllib2.urlopen(req)

output = open('out.jpg','wb')
output.write(response.read())
output.close()

This is the solution of your problem.

Upvotes: 1

Related Questions