zoukankan      html  css  js  c++  java
  • urllib2使用

    urllib2是一个类似curl的Python扩展,默认已经安装。官网:http://docs.python.org/2/library/urllib2.html

    The urllib2 module defines functions and classes which help in opening URLs (mostly HTTP) in a complex world — basic and digest authentication, redirections, cookies and more.

    urllib2.urlopen(url[, data][, timeout])

    Open the URL url, which can be either a string or a Request object.

    This function returns a file-like object with two additional methods。

    class urllib2.Request(url[, data][, headers][, origin_req_host][, unverifiable])

    This class is an abstraction of a URL request.

    import urllib2
    req=urllib2.Request("http://www.baidu.com")
    response=urllib2.urlopen(req)
    html=response.read()

    或者

    import urllib2
    >>> html = urllib2.urlopen('http://某piratebay.se/browse/200').read()

    http://docs.python.org/2/library/urllib2.html
    Post传参:
    import urllib
    import urllib2
    
    url='http://localhost/php/GetPost.php'
    
    dataArr={'name':'jack','password':'pass'}
    data=urllib.urlencode(dataArr)#encode a sequence of two tuple or dict
    
    req=urllib2.Request(url,data)
    res=urllib2.urlopen(req)
    html=res.read()

    Get传参:

    只要在请求的url附带?参数就可以。 

    import urllib
    import urllib2
    
    url='http://localhost/php/GetPost.php'
    dataArr={'name':'jack','password':'pass'}
    data=urllib.urlencode(dataArr)#encode a sequence of two tuple or dict
    
    full_url = url + '?' + data
     
    req=urllib2.Request(full_url)
    res=urllib2.urlopen(req)
    html=res.read()

    设置Headers到http请求

    有一些站点不喜欢被程序(非人为访问)访问,或者发送不同版本的内容到不同的浏览器。

    默认的urllib2把自己作为“Python-urllib/x.y”(x和y是Python主版本和次版本号,例如Python-urllib/2.7),

    这个身份可能会让站点迷惑,或者干脆不工作。

    浏览器确认自己身份是通过User-Agent头(http://baike.baidu.com/view/3398471.htm),当你创建了一个请求对象,你可以给他一个包含头数据的字典。

    下面的例子发送跟上面一样的内容,但把自身模拟成Internet Explorer。

    import urllib  
    import urllib2  
    
    url = 'http://www.someserver.com/cgi-bin/register.cgi'
    
    user_agent = 'Mozilla/4.0 (compatible; MSIE 5.5; Windows NT)'  
    values = {'name' : 'WHY',  
              'location' : 'SDU',  
              'language' : 'Python' }  
    
    headers = { 'User-Agent' : user_agent }  
    data = urllib.urlencode(values)  
    req = urllib2.Request(url, data, headers)  
    response = urllib2.urlopen(req)  
    the_page = response.read() 

    IE10的user agent

    Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.2; Trident/6.0)

    下面的很大一部分内容都来自:http://www.voidspace.org.uk/python/articles/urllib2.shtml
    urllib2 - The Missing Manual HOWTO Fetch Internet Resources with Python

    urlopen raises URLError when it cannot handle a response (though as usual with Python APIs, builtin exceptions such as ValueError, TypeError etc. may also be raised).

    HTTPError is the subclass of URLError raised in the specific case of HTTP URLs.

    URLError

    Often, URLError is raised because there is no network connection (no route to the specified server), or the specified server doesn't exist. In this case, the exception raised will have a 'reason' attribute, which is a tuple containing an error code and a text error message.

    req=urllib2.Request("http://www.pretend_server.org")
    try:urllib2.urlopen(req)
    except urllib2.URLError,e:
        print e.reason #没有e.code属性

    .HTTPError
    服务器上每一个HTTP 应答对象response包含一个数字"状态码"。

    有时状态码指出服务器无法完成请求。默认的处理器会为你处理一部分这种应答。

    例如:假如response是一个"重定向",需要客户端从别的地址获取文档,urllib2将为你处理。

    其他不能处理的,urlopen会产生一个HTTPError。

    典型的错误包含"404"(页面无法找到),"403"(请求禁止),和"401"(带验证请求)。

    HTTP状态码表示HTTP协议所返回的响应的状态。

    比如客户端向服务器发送请求,如果成功地获得请求的资源,则返回的状态码为200,表示响应成功。

    如果请求的资源不存在, 则通常返回404错误。 

    HTTP状态码通常分为5种类型,分别以1~5五个数字开头,由3位整数组成:

    ------------------------------------------------------------------------------------------------

    200:请求成功      处理方式:获得响应的内容,进行处理 

    201:请求完成,结果是创建了新资源。新创建资源的URI可在响应的实体中得到    处理方式:爬虫中不会遇到 

    202:请求被接受,但处理尚未完成    处理方式:阻塞等待 

    204:服务器端已经实现了请求,但是没有返回新的信 息。如果客户是用户代理,则无须为此更新自身的文档视图。    处理方式:丢弃

    300:该状态码不被HTTP/1.0的应用程序直接使用, 只是作为3XX类型回应的默认解释。存在多个可用的被请求资源。    处理方式:若程序中能够处理,则进行进一步处理,如果程序中不能处理,则丢弃
    301:请求到的资源都会分配一个永久的URL,这样就可以在将来通过该URL来访问此资源    处理方式:重定向到分配的URL
    302:请求到的资源在一个不同的URL处临时保存     处理方式:重定向到临时的URL 

    304 请求的资源未更新     处理方式:丢弃 

    400 非法请求     处理方式:丢弃 

    401 未授权     处理方式:丢弃 

    403 禁止     处理方式:丢弃 

    404 没有找到     处理方式:丢弃 

    5XX 回应代码以“5”开头的状态码表示服务器端发现自己出现错误,不能继续执行请求    处理方式:丢弃

    ------------------------------------------------------------------------------------------------

    HTTPError实例产生后会有一个整型'code'属性,是服务器发送的相关错误号。

    Error Codes错误码
    因为默认的处理器处理了重定向(300以外号码),并且100-299范围的号码指示成功,所以你只能看到400-599的错误号码。
    BaseHTTPServer.BaseHTTPRequestHandler.response是一个很有用的应答号码字典,显示了HTTP协议使用的所有的应答号。

    当一个错误号产生后,服务器返回一个HTTP错误号,和一个错误页面。

    你可以使用HTTPError实例作为页面返回的应答对象response。

    这表示和错误属性一样,它同样包含了read,geturl,和info方法。

    我们建一个urllib2_test07.py来感受一下:

    import urllib2
    req = urllib2.Request('http://bbs.csdn.net/callmewhy')
    
    try:
        urllib2.urlopen(req)
    
    except urllib2.URLError, e:
    
        print e.code
        #print e.read()

    下F5可以看见输出了404的错误码,也就说没有找到这个页面(由于httperror是urlerror子类

    同样有e.reason属性

    比如e.code 为404时,e.reason为not found。

    总结:

    所以如果你想为HTTPError或URLError做准备,将有两个基本的办法。推荐使用第二种。

     我们建一个urllib2_test08.py来示范一下第一种异常处理的方案:

    from urllib2 import Request, urlopen, URLError, HTTPError
    
    req = Request('http://bbs.csdn.net/callmewhy')
    
    try:
    
        response = urlopen(req)
    
    except HTTPError, e:
    
        print 'The server couldn\'t fulfill the request.'
    
        print 'Error code: ', e.code
    
    except URLError, e:
    
        print 'We failed to reach a server.'
    
        print 'Reason: ', e.reason
    
    else:
        print 'No exception was raised.'
        # everything is fine
    和其他语言相似,try之后捕获异常并且将其内容打印出来。
    这里要注意的一点,except HTTPError 必须在第一个,否则except URLError将同样接受到HTTPError 。
    因为HTTPError是URLError的子类,如果URLError在前面它会捕捉到所有的URLError(包括HTTPError )。
     

    我们建一个urllib2_test09.py来示范一下第二种异常处理的方案:

    from urllib2 import Request, urlopen, URLError, HTTPError
    
    req = Request('http://bbs.csdn.net/callmewhy')
      
    try:  
      
        response = urlopen(req)  
      
    except URLError, e:  
      
        if hasattr(e, 'reason'):  
      
            print 'We failed to reach a server.'  
      
            print 'Reason: ', e.reason  
      
        elif hasattr(e, 'code'):  
      
            print 'The server couldn\'t fulfill the request.'  
      
            print 'Error code: ', e.code  
      
    else:  
        print 'No exception was raised.'  
        # everything is fine  

    主要利用了第一个HttpError有e.code属性.

    运行结果:(由于是HttpError错误,有e.code)

    The server couldn't fulfill the request.
    Error code: 404

    Opener与Handler的介绍和实例应用

    在开始后面的内容之前,先来解释一下urllib2中的两个个方法:info and geturl 

    urlopen返回的应答对象response(或者HTTPError实例)有两个很有用的方法info()和geturl()

    1.geturl():

    这个返回获取的真实的URL,这个很有用,因为urlopen(或者opener对象使用的)或许会有重定向。获取的URL或许跟请求URL不同。

    以人人中的一个超级链接为例,

    我们建一个urllib2_test10.py来比较一下原始URL和重定向的链接

    from urllib2 import Request, urlopen, URLError, HTTPError
    
    
    old_url = 'http://rrurl.cn/b1UZuP'
    req = Request(old_url)
    response = urlopen(req)  
    print 'Old url :' + old_url
    print 'Real url :' + response.geturl()

    运行之后可以看到真正的链接指向的网址:

    old_urlhttp://rrurl.cn/b1UZuP
    Real_url:http://www.polyu.edu.hk/polyuchallenge/best_of_the_best_elevator_pitch_award/bbca_voting_process.php?voted_team_id=670&section=1&bbca_year_id=3

    info():

    这个返回对象的字典对象,该字典描述了获取的页面情况。通常是服务器发送的特定头headers。目前是httplib.HTTPMessage 实例。

    经典的headers包含"Content-length","Content-type",和其他内容。

    我们建一个urllib2_test11.py来测试一下info的应用

    from urllib2 import Request, urlopen, URLError, HTTPError
    
    old_url = 'http://www.baidu.com'
    req = Request(old_url)
    response = urlopen(req)  
    print 'Info():'
    print response.info()

    下面来说一说urllib2中的两个重要概念:Openers和Handlers。

    1.Openers:

    当你获取一个URL你使用一个opener(一个urllib2.OpenerDirector的实例)。

    正常情况下,我们使用默认opener:通过urlopen。

    但你能够创建个性的openers。

    2.Handles:

    Openers使用处理器handlers,所有的“繁重”工作由handlers处理。

    每个handlers知道如何通过特定协议打开URLs,或者如何处理URL打开时的各个方面。

    例如HTTP重定向或者HTTP cookies。

     

    如果你希望用特定处理器获取URLs你会想创建一个openers,例如获取一个能处理cookie的opener,或者获取一个不重定向的opener。

     

    要创建一个 opener,可以实例化一个OpenerDirector,

    然后调用.add_handler(some_handler_instance)。

    同样,可以使用build_opener,这是一个更加方便的函数,用来创建opener对象,他只需要一次函数调用。
    build_opener默认添加几个处理器,但提供快捷的方法来添加或更新默认处理器。

    其他的处理器handlers你或许会希望处理代理,验证,和其他常用但有点特殊的情况。

    install_opener 用来创建(全局)默认opener。这个表示调用urlopen将使用你安装的opener。

    Opener对象有一个open方法。

    该方法可以像urlopen函数那样直接用来获取urls:通常不必调用install_opener,除了为了方便。

     

    说完了上面两个内容,下面我们来看一下基本认证的内容,这里会用到上面提及的Opener和Handler。

    Basic Authentication 基本验证

    为了展示创建和安装一个handler,我们将使用HTTPBasicAuthHandler。

    当需要基础验证时,服务器发送一个header(401错误码) 请求验证。这个指定了scheme 和一个‘realm’,看起来像这样:Www-authenticate: SCHEME realm="REALM".

    例如
    Www-authenticate: Basic realm="cPanel Users"

    客户端必须使用新的请求,并在请求头里包含正确的姓名和密码。

    这是“基础验证”,为了简化这个过程,我们可以创建一个HTTPBasicAuthHandler的实例,并让opener使用这个handler就可以啦。


    HTTPBasicAuthHandler使用一个密码管理的对象来处理URLs和realms来映射用户名和密码。

    如果你知道realm(从服务器发送来的头里)是什么,你就能使用HTTPPasswordMgr。


    通常人们不关心realm是什么。那样的话,就能用方便的HTTPPasswordMgrWithDefaultRealm。

    这个将在你为URL指定一个默认的用户名和密码。

    这将在你为特定realm提供一个其他组合时得到提供。

    我们通过给realm参数指定None提供给add_password来指示这种情况。

     

    最高层次的URL是第一个要求验证的URL。你传给.add_password()更深层次的URLs将同样合适。

    说了这么多废话,下面来用一个例子演示一下上面说到的内容。

    我们建一个urllib2_test12.py来测试一下info的应用:

    # -*- coding: utf-8 -*-
    import urllib2
    
    # 创建一个密码管理者
    password_mgr = urllib2.HTTPPasswordMgrWithDefaultRealm()
    
    # 添加用户名和密码
    
    top_level_url = "http://example.com/foo/"
    
    # 如果知道 realm, 我们可以使用他代替 ``None``.
    # password_mgr.add_password(None, top_level_url, username, password)
    password_mgr.add_password(None, top_level_url,'why', '1223')
    
    # 创建了一个新的handler
    handler = urllib2.HTTPBasicAuthHandler(password_mgr)
    
    # 创建 "opener" (OpenerDirector 实例)
    opener = urllib2.build_opener(handler)
    
    a_url = 'http://www.baidu.com/'
    
    # 使用 opener 获取一个URL
    opener.open(a_url)
    
    # 安装 opener.
    # 现在所有调用 urllib2.urlopen 将用我们的 opener.
    urllib2.install_opener(opener)
    
     

    注意:以上的例子我们仅仅提供我们的HHTPBasicAuthHandler给build_opener。

    默认的openers有正常状况的handlers:ProxyHandler,UnknownHandler,HTTPHandler,HTTPDefaultErrorHandler, HTTPRedirectHandler,FTPHandler, FileHandler, HTTPErrorProcessor。

    代码中的top_level_url 实际上可以是完整URL(包含"http:",以及主机名及可选的端口号)。

    例如:http://example.com/。

    也可以是一个“authority”(即主机名和可选的包含端口号)。

    例如:“example.com” or “example.com:8080”。

    后者包含了端口号。

    深入:

    http://www.voidspace.org.uk/python/articles/urllib2.shtml

  • 相关阅读:
    第二阶段第九天
    第二阶段第八天
    一轮项目冲刺——移山小分队(3)
    一轮项目冲刺——移山小分队(2)
    典型用户和用户场景
    一轮项目冲刺——移山小分队
    寻找水王
    NABCD需求分析
    每日记录01
    二维数组最大连通和的求解
  • 原文地址:https://www.cnblogs.com/youxin/p/3064434.html
Copyright © 2011-2022 走看看