1

I've tried to get the image from the following url.

http://upic.me/i/fj/the_wonderful_mist_once_again_01.jpg

I can do right-click and save-as but when I tried to use urlretrieve like

import urllib
img_url = 'http://upic.me/i/fj/the_wonderful_mist_once_again_01.jpg'
urllib.urlretrieve( img_url, 'cover.jpg')

I found that it is html instead of .jpg image but I don't know why. Could you please tell me why does my method not work? Are there any option that can mimic right-click save-as method?

Hackaholic
  • 19,069
  • 5
  • 54
  • 72
Winnie
  • 13
  • 4

2 Answers2

0

try like this:

import urllib2

image = urllib2.urlopen('http://upic.me/i/fj/the_wonderful_mist_once_again_01.jpg').read()
f = open('some_name.jpg','w')
f.write(image)
f.close()
Hackaholic
  • 19,069
  • 5
  • 54
  • 72
  • It doesn't work neither. The image is broken but if I save it as html, it's readable and it is actually html. – Winnie Apr 03 '15 at 14:29
0

You can use Requests, if you havn't installed yet, pip install requests

Because this img_url was redirected by the server to another html page ( that was the html page you just downloaded) if you didn't provide a referer header.

So the following code first find the redirect url, and add it to the HTTP Referer header.

import requests
img_url = 'http://upic.me/i/fj/the_wonderful_mist_once_again_01.jpg'

r = requests.get(img_url, allow_redirects=False)   #  stop redirect 302 , capture redirects url

headers = {}
headers['Referer'] = r.headers['location']    # add this url to referer 'http://upic.me/show/55132055'

r = requests.get(img_url, headers=headers)
filename = img_url.split('/')[-1]             # find the file name in `img_url`
with open(filename, 'wb') as fh:             # use 'wb' to write in binary mode
    fh.write(r.content)
Aaron
  • 2,383
  • 3
  • 22
  • 53
  • 1
    Thanks, it does really work! Very Thanks for clear explanation. A senior told me to change 'http' to 'https' and it works also. Unfortunately, I have no chance to ask him for details. – Winnie Apr 03 '15 at 15:46