问题:

 urllib.request.urlopen() 方法经常会被用来打开一个网页的源代码,然后会去分析这个页面源代码,但是对于有的网站使用这种方法时会抛出"HTTP Error 403: Forbidden"异常
例如 执行下面的语句时
[python] 
<span style="font-size:14px;"> urllib.request.urlopen("http://blog.csdn.net/eric_sunah/article/details/11099295")</span>  
 
会出现以下异常:
[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, response, code, msg, hdrs)  
  File "D:\Python32\lib\urllib\request.py", line 513, in error  
    return 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>  
分析:
之所以出现上面的异常,是因为如果用 urllib.request.urlopen 方式打开一个URL,服务器端只会收到一个单纯的对于该页面访问的请求,但是服务器并不知道发送这个请求使用的浏览器,操作系统,硬件平台等信息,而缺失这些信息的请求往往都是非正常的访问,例如爬虫.
有些网站为了防止这种非正常的访问,会验证请求信息中的UserAgent(它的信息包括硬件平台、系统软件、应用软件和用户个人偏好),如果UserAgent存在异常或者是不存在,那么这次请求将会被拒绝(如上错误信息所示)
所以可以尝试在请求中加入UserAgent的信息
方案:
对于Python 3.x来说,在请求中添加UserAgent的信息非常简单,代码如下
[python]  
#如果不加上下面的这行出现会出现urllib2.HTTPError: HTTP Error 403: Forbidden错误  
    #主要是由于该网站禁止爬虫导致的,可以在请求加上头信息,伪装成浏览器访问User-Agent,具体的信息可以通过火狐的FireBug插件查询  
    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()  
 
将urllib.request.urlopen.read() 替换成上面的代码后,对于出现问题的页面就可以就正常访问
 

urllib.error.HTTPError: HTTP Error 403: Forbidden的更多相关文章

  1. python抓取不得姐动图(报错 urllib.error.HTTPError: HTTP Error 403: Forbidden)

    抓取不得姐动图(报错) # -*- coding:utf-8 -*- #__author__ :kusy #__content__:文件说明 #__date__:2018/7/23 17:01 imp ...

  2. python之urllib.request.urlopen(url)报错urllib.error.HTTPError: HTTP Error 403: Forbidden处理及引申浏览器User Agent处理

    最近在跟着院内大神学习python的过程中,发现使用urllib.request.urlopen(url)请求服务器是报错: 在园子里找原因,发现原因为: 只会收到一个单纯的对于该页面访问的请求,但是 ...

  3. python3 raise HTTPError(req.full_url, code, msg, hdrs, fp) urllib.error.HTTPError: HTTP Error 403: Forbid

    1.分析: 如果用 urllib.request.urlopen 方式打开一个URL,服务器端只会收到一个单纯的对于该页面访问的请求,但是服务器并不知道发送这个请求使用的浏览器,操作系统,硬件平台等信 ...

  4. jenkins连接提示错误urllib.error.HTTPError: HTTP Error 403

    昨天在执行python连接Jenkins获取编译失败日志失败时,出现错误,具体报错如下,主要是在连接问题上的问题,做了一个请求 就提示错误 原因在于Jenkins的权限,或者访问页面的url需要进行登 ...

  5. Python "HTTP Error 403: Forbidden"

    问题: 执行下面的语句时 def set_IPlsit(): url = 'https://www.whatismyip.com/' response = urllib.request.urlopen ...

  6. python3 HTTP Error 403:Forbidden

    问题描述初学python,在用python中的urllib.request.urlopen()和urllib.request.urlretrieve方法打开网页时,有些网站会抛出异常: HTTP Er ...

  7. 解决github push错误The requested URL returned error: 403 Forbidden while accessing

    来源:http://blog.csdn.net/happyteafriends/article/details/11554043 github push错误: git push error: The  ...

  8. 解决git提交问题error: The requested URL returned error: 403 Forbidden while accessing

    git提交代码时,出现这个错误"error: The requested URL returned error: 403 Forbidden while accessing https&qu ...

  9. PYCURL ERROR 22 - "The requested URL returned error: 403 Forbidden"

    RHEL6.5创建本地Yum源后,发现不可用,报错如下: [root@namenode1 html]# yum install gcc Loaded plugins: product-id, refr ...

随机推荐

  1. Counting Rectangles

    Counting Rectangles Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 1043 Accepted: 546 De ...

  2. winform之自定义控件

    这样的一个控件 肯定得通过自定义控件来实现了 public class ProcessLabel : Control { public ProcessLabel() { //InitializeCom ...

  3. jquery简单插件到复杂插件(3)--顶部导航固定

    那个效果很多,比如hao123的头部 <!DOCTYPE html> <html> <head> <meta charset="UTF-8" ...

  4. Easyui部分组件讲解

    Easyui部分组件讲解 目  录 1.... Accordion(可折叠标签)... 2 1.1          实例... 2 1.2          参数... 3 2.... DateBo ...

  5. sqlitehelper封装

    appsettings <configuration>    <appSettings>        <add key="ConnectionString&q ...

  6. ContentProvider官方教程(1)何时用content provider

    Content Providers Content providers manage access to a structured set of data. They encapsulate the ...

  7. Struts2的输入校验(1)——校验规则文件的编写

    Struts2的输入校验(1) --校验规则文件的编写 Struts2提供了基于验证框架的输入校验,所有的输入校验只要编写配置文件,Struts2的验证框架将会负责进行服务器校验和客户端校验. 注: ...

  8. nl2br

    PHP中   在字符串所有新行之前插入 HTML 换行标记 说明 string nl2br ( string $string [, bool $is_xhtml = true ] ) 在字符串 str ...

  9. MusigCV安装

    首先,将下载的安装文件zip包,http://www.mathworks.com/products/compiler/mcr/ MCR2013a 然后依次执行下面的命令: 进入目录:cd /tmp 解 ...

  10. eclipse下添加viplugin插件的方法

    http://www.viplugin.com/ 在eclipse根目录下建立文件:viplugin2.lic,然后在里面添加以下字符串: nd4UFjUMBADcUSeSW8ocLKoGP3lpbW ...