Try to download image from image url, but get html instead
<p>similar to <a href="http://stackoverflow.com/questions/29433699/try-to-scrape-image-from-image-url-using-python-urllib-but-get-html-instead">Try to scrape image from image url (using python urllib ) but get html instead</a> , but the solution does not work for me.</p>

<pre><code>from BeautifulSoup import BeautifulSoup
import urllib2
import requests

img_url='http://7-themes.com/data_images/out/79/7041933-beautiful-backgrounds-wallpaper.jpg'

r = requests.get(img_url, allow_redirects=False)

headers = {}
headers['Referer'] = r.headers['location']

r = requests.get(img_url, headers=headers)
with open('7041933-beautiful-backgrounds-wallpaper.jpg', 'wb') as fh:
    fh.write(r.content)
</code></pre>

<p>the downloaded file is still a html page, not an image.</p>

<p>I found a root cause in my code is that refer field in the header is still a html, not image.</p>

<p>So I change the refer field to the <code>img_url</code>, and this works.</p>

<pre><code>from BeautifulSoup import BeautifulSoup
import urllib2
import urllib
import requests

img_url='http://7-themes.com/data_images/out/79/7041933-beautiful-backgrounds-wallpaper.jpg'

headers = {}
headers['Referer'] = img_url

r = requests.get(img_url, headers=headers)

with open('7041933-beautiful-backgrounds-wallpaper.jpg', 'wb') as fh:
    fh.write(r.content)
</code></pre>

python httprequest urllib2