Urllib.error.HTTPError:HTTP Error 403:forbidden

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.