This occurs in the crawler process:
Traceback (most recent call last):
File "D:\pythonDemo01\pythonDemo01\Demo19.py", line 11, in <module>
response=urllib2.urlopen(imag_url)
File "D:\Python27\lib\urllib2.py", line 154, in urlopen
return opener.open(url, data, timeout)
File "D:\Python27\lib\urllib2.py", line 435, in open
response = meth(req, response)
File "D:\Python27\lib\urllib2.py", line 548, in http_response
'http', request, response, code, msg, hdrs)
File "D:\Python27\lib\urllib2.py", line 473, in error
return self._call_chain(*args)
File "D:\Python27\lib\urllib2.py", line 407, in _call_chain
result = func(*args)
File "D:\Python27\lib\urllib2.py", line 556, in http_error_default
raise HTTPError(req.get_full_url(), code, msg, hdrs, fp)
urllib2.HTTPError: HTTP Error 403: Forbidden
Code:
imag_url='http://img4.imgtn.bdimg.com/it/u=495525449,2738183538&fm=214&gp=0.jpg'
response=urllib2.urlopen(imag_url)
with open('nv.jpg','wb') as f:
f.write(response.read())
Baidu said the site has banned crawlers because we haven’t disguised ourselves as a browser yet:
It seems that we need to add something related to the request header:
headers={
'Accept-Language': 'zh-CN',
'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Trident/7.0; rv:11.0) like Gecko'
}
imag_url='http://img4.imgtn.bdimg.com/it/u=495525449,2738183538&fm=214&gp=0.jpg'
res=urllib2.Request(url=imag_url,headers=headers)
response=urllib2.urlopen(res)
with open('nv.jpg','wb') as f:
f.write(response.read())
div>
Read More:
- Urllib2.httperror: http error 403: forbidden solution
- raise HTTPError(req.full_url, code, msg, hdrs, fp)urllib.error.HTTPError: HTTP Error 404: Not Found
- Python crawler: urllib.error.HTTPError : HTTP Error 404: Not Found
- Python crawler urllib.error.HTTPError : HTTP Error 418:
- HTTPError HTTP Error 500 INTERNAL SERVER ERROR
- urlopen error unknown url type:httpë/HTTP Error 400:Bad Request
- Solving attributeerror: module ‘urllib’ has no attribute ‘request’
- python:urllib2.URLError urlopen error [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed
- python: HTTP Error 505: HTTP Version Not Supported
- Error 403 Forbidden when using WGet or curl
- urllib.error.URLError: <urlopen error [WinError 10060] problem solving
- Attributeerror: the solution of module ‘urllib’ has no attribute ‘quote’
- Forbidden (403) CSRF verification failed. Request aborted. – Django
- Solve GitHub push error 403 Forbidden while accessing
- urllib3 (1.26.4) or chardet (4.0.0) doesn‘t match a supported version!
- Nextcloud error: “access forbidden CSRF check failed”
- GitHub push ErrorThe requested URL returned error: 403 Forbidden while accessing
- Copy forbidden in C + +++
- Error parsing HTTP request header Note: further occurrences of HTTP header p
- Springboot project: error parsing HTTP request header note: further occurrences of HTTP request parsing