Problem:
The urllib.request.urlopen () method is often used to open the source code of a Web page, and then parses the page source code, but for some sites using this method throws an "HTTP Error 403:forbidden" Exception for example Execute the following statement when [Python] <span style= "FONT-SIZE:14PX;" > Urllib.request.urlopen ("http://blog.csdn.net/eric_sunah/article/details/11099295") </span> The following exception appears: [Python] <span style= "color: #FF0000;" > file "D:\Python32\lib\urllib\request.py", line 475, in open response = meth (req, response) File "D:\Python32\lib\urllib\request.py", line 587, in Http_response ' http ', request, re Sponse, Code, MSG, HDRs) File "D:\Python32\lib\urllib\request.py", line 513, in error RET Urn Self._call_chain (*args) File "D:\Python32\lib\urllib\request.py", line 447, in _call_chain result = Func (*args) File "D:\Python32\lib\urllib\request.py", line 595, in Http_error_default Raise Httperror (Req.full_url, Code, MSG, HDRS, FP) urllib.error.httperror:http error 403:forbidden</span> Analysis: The above exception occurs because if Urllib.request.urlopen open a URL, the server will only receive a simple request for access to the page, but the server does not know to send this request using the browser, operating system, hardware platform and other information, Requests for missing this information are often non-normal access, such as crawlers. Some websites, in order to prevent this unusual access, will verify the useragent in the requested information (including hardware platforms, system software, application software, and user preferences), If the useragent is an exception or does not exist, then this request will be rejected (as shown in the error message) so you can try to add useragent information scheme to the request: for python 3.x, Adding useragent information to the request is straightforward, with the code as follows [Python] #如果不加上下面的这行出现会出现urllib2. Httperror:http error 403:forbidden errors #主要是由于该网站禁止爬虫导致的, can be in the request to add header information, disguised as a browser access user-agent, Specific information can be queried via Firefox's firebug plugin headers = {' user-agent ': ' mozilla/5.0 (Windows NT 6.1; WOW64; rv:23.0) gecko/20100101 firefox/23.0 '} req = Urllib.request.Request (Url=chaper_url, headers= Headers) Urllib.request.urlopen (req). Read () will Urllib.request.urlopen.read () Replace with the above code, for the page that has the problem can be normal access transfer from http://www.2cto.com/kf/201309/242273.html
Urllib.error.HTTPError:HTTP Error 403:forbidden