您可以使用Requests,如果你没有带装的是,pip install requests
因为这img_url
由服务器到另一个HTML页面重定向(那是HTML如果您没有提供referer
标题,则表示您刚刚下载的页面)。
因此,下面的代码首先找到重定向url,并将其添加到HTTP Referer头。
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)
谢谢,它真的有用!非常感谢您的明确解释。 一位资深人士告诉我把'http'改成'https',它也可以。不幸的是,我没有机会向他询问细节。 – Winnie 2015-04-03 15:46:59