与Try to scrape image from image url (using python urllib ) but get html instead类似,但解决方案对我不起作用。
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)
下载的文件仍然是html页面,而不是图像。
答案 0 :(得分:0)
您的推荐人未正确设置。我已经硬编码了推荐人并且工作正常
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'] = 'http://7-themes.com/7041933-beautiful-backgrounds-wallpaper.html'
r = requests.get(img_url, headers=headers, allow_redirects=False)
with open('7041933-beautiful-backgrounds-wallpaper.jpg', 'wb') as fh:
fh.write(r.content)
答案 1 :(得分:0)
我在代码中发现根本原因是标题中的引用字段仍然是html,而不是图像。
所以我将引用字段更改为addon/mixins
,这样就可以了。
img_url