具体实现原来可以参考这个链接: https://www.zhihu.com/question/35044484
下面给个Django下的实现代码:
@csrf_exempt
def image_proxy(request):
img = request.GET.get('img')
headers = {
'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.108 Safari/537.36',
}
status = 0
try:
r = requests.get(
img,
headers=headers)
except ConnectionError, ConnectTimeout:
status = 1
if status == 1:
return ''
response = HttpResponse(r.content, content_type='image/jpeg')
return response
url.py
url(r'^spider-api/image-proxy/$', image_proxy),
访问方法,url:
http://127.0.0.1:8001/spider-api/image-proxy/?img=https://mmbiz.qpic.cn/mmbiz_png/WliaoSKPrpSPqGrhMmQK8MwKR6AZ7qDDy2JtSxRjk3ZUke41PUGP6RoaibzIgxw8ey5cejb5FzkplhgGd48oOxAg/640