python爬取图片提示urllib.error.HTTPError: HTTP Error 403: Forbidden解决

最近在使用python爬去妹子图片的时候,抛出了Traceback (most recent call last):
File "meinv.py", line 108, in
main()
File "meinv.py", line 104, in main
imghandle.run()
File "meinv.py", line 96, in run
self.handle_data(response)
File "meinv.py", line 84, in handle_data
self.handle_imgdata(res, iname)
File "meinv.py", line 65, in handle_imgdata
urllib.request.urlretrieve(imgurl, filepath)
File "D:\allkitinstall\python3.5.3\lib\urllib\request.py", line 188, in urlretrieve
with contextlib.closing(urlopen(url, data)) as fp:
File "D:\allkitinstall\python3.5.3\lib\urllib\request.py", line 163, in urlopen
return opener.open(url, data, timeout)
File "D:\allkitinstall\python3.5.3\lib\urllib\request.py", line 472, in open
response = meth(req, response)
File "D:\allkitinstall\python3.5.3\lib\urllib\request.py", line 582, in http_response
'http', request, response, code, msg, hdrs)
File "D:\allkitinstall\python3.5.3\lib\urllib\request.py", line 510, in error
return self._call_chain(*args)
File "D:\allkitinstall\python3.5.3\lib\urllib\request.py", line 444, in _call_chain
result = func(*args)
File "D:\allkitinstall\python3.5.3\lib\urllib\request.py", line 590, in http_error_default
raise HTTPError(req.full_url, code, msg, hdrs, fp)
urllib.error.HTTPError: HTTP Error 403: Forbidden异常

先贴上代码:

这是构建请求对象的函数

python爬取图片提示urllib.error.HTTPError: HTTP Error 403: Forbidden解决_第1张图片

这里是发送请求并下载图片的代码

python爬取图片提示urllib.error.HTTPError: HTTP Error 403: Forbidden解决_第2张图片

上网查了很多资料,都说是缺少user-agent头,但我明明是已经加上了,尝试了很多遍还是没有用,就在我快要放弃的时候,在抓包工具中突然发现了图片请求头中的一个头信息Referer,这才突然想起来

很多网站都会设置Referer头来做防盗链功能,防止网站被恶意的请求,于是我很开心的加上这个请求头

python爬取图片提示urllib.error.HTTPError: HTTP Error 403: Forbidden解决_第3张图片

再次进行尝试

python爬取图片提示urllib.error.HTTPError: HTTP Error 403: Forbidden解决_第4张图片

问题被完美的解决!

初学python,如有错误请多多指正!

 

转载于:https://www.cnblogs.com/cwhome/p/11038593.html

你可能感兴趣的:(python爬取图片提示urllib.error.HTTPError: HTTP Error 403: Forbidden解决)