Friday, September 6, 2013

How to download the correct image with mechanize

How to download the correct image with mechanize

How can I use mechanize to download an image whose url stays the same but
the image changes on each request?
This is what i have at the moment but it puts in another request to the
url of the image so a new image is saved? How can I save the image I am
presented with on the first load of the page?
begin
a.get('http://www.google.co.uk/sorry/?continue=http://www.google.co.uk/search%3Fq%3Dhello%26oq%3Dhello%26ie%3DUTF-8')
rescue Mechanize::ResponseCodeError => e
image = e.page.search('img')[0]['src']
imageurl = 'http://www.google.co.uk' + image
agent.get(imageurl).save "path/to/folder/image_name.jpg"
end
This is an example of the image url you are presented with, on each
refresh the image changes.
http://www.google.co.uk/sorry/image?id=13540456516242973445

No comments:

Post a Comment