urllib

2017-11-21  本文已影响0人  道无虚

python模块(包)之urllib

urllib:官方文档是最好的模块表达说明。

urllib is a package that collects several modules for working with URLs:

大体来说就是urllib是一个包含request、error、parse、robotparser四个模块,关乎网络资源请求的包。request模块用来发起网络资源请求;error模块用来在request网络资源过程中搜集异常报错;parse模块用来对url地址进行处理;robotparser模块用来解析robots.txt文件(未知)。

1、request

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

1.1 方法

urllib.request.urlopen(url, data=None, [timeout, ], cafile=None, capath=None, cadefault=False, context=None)

这里一般url、data、timeout三个参数还比较常用。

该函数返回一个上下文管理对象,包含一下几种方法获取返回结果的相关信息:

对于http和https,除上述的几个函数获取信息外,该函数返回也是对http.client.HTTPResponse稍加修改的对象,其详细说明见下方官档。

HTTPResponse Objects 官档

HTTPResponse Objects这个对象常用方法有:

在查看urllib的源码request.py找到类OpenerDirector,其下面方法open可以找到下面几行代码:

if isinstance(fullurl, str):
    req = Request(fullurl, data)
else:
    req = fullurl
    if data is not None:
        req.data = data

urllib.request.urlopen() 方面介绍里面提到:请求资源可以是url地址字符串,或者是Request对象。再看上面一段代码isinstance(fullurl, str)传递的fullurl是str的实例对象,就将fullurl等转化为Request的实例对象。

所以请求资源是url地址字符串或是Request对象都殊途同归,最终会转为Request的实例对象进行资源请求。下面的子类会对Request对象进行详细介绍。

附加这个方法对应的去注释源码。

def urlopen(url, data=None, timeout=socket._GLOBAL_DEFAULT_TIMEOUT,
            *, cafile=None, capath=None, cadefault=False, context=None):
    global _opener
    if cafile or capath or cadefault:
        import warnings
        warnings.warn("cafile, cpath and cadefault are deprecated, use a "
                      "custom context instead.", DeprecationWarning, 2)
        if context is not None:
            raise ValueError(
                "You can't pass both context and any of cafile, capath, and "
                "cadefault"
            )
        if not _have_ssl:
            raise ValueError('SSL support not available')
        context = ssl.create_default_context(ssl.Purpose.SERVER_AUTH,
                                             cafile=cafile,
                                             capath=capath)
        https_handler = HTTPSHandler(context=context)
        opener = build_opener(https_handler)
    elif context:
        https_handler = HTTPSHandler(context=context)
        opener = build_opener(https_handler)
    elif _opener is None:
        _opener = opener = build_opener()
    else:
        opener = _opener

这里面注意几个变量。

a、https_handler:姑且称为资源构造器,它相当于处理不同网络资源的句柄对象,如HTTPHandler、HTTPSHandler、FileHandler、FTPHandler、UnknownHandler等类的实例对象。

b、opener = build_opener(https_handler):姑且称为资源钥匙,它是一个OpenerDirector类的实例对象,其参数是上面说的资源构造器,用这把钥匙可以打开网络的任意资源。

下面继续request模块的方法介绍。

urllib.request.build_opener([handler, ...]):构造资源钥匙,它是一个OpenerDirector类的实例对象,其参数是上面说的资源构造器。

urllib.request.install_opener(opener):插入资源钥匙,载入OpenerDirector子类的实例对象,用来请求网络资源。

1.2 request常用子类

urllib.request.Request(url, data=None, headers={}, origin_req_host=None, unverifiable=False, method=None):网络资源请求的抽象。

urllib.request.HTTPCookieProcessor(cookiejar=None):处理http cookie。

urllib.request.ProxyHandler(proxies=None):代理请求。

urllib.request.FileHandler():一个文件对象。(不知是否可以作为上传文件使用。)

这些子类又有一些自己的方法,大多暂且不介绍,附Request类的方法官档链接。

Request类的方法官档

2、error

处理由request请求产生的错误。

urllib.error.URLError:地址错误,有属性如下:

urllib.error.HTTPError:网络请求错误,有属性如下:

3、parse

The urllib.parse module defines functions that fall into two broad categories: URL parsing and URL quoting. These are covered in detail in the following sections.

这个模块提供处理url的标准接口,两种:解析处理和引用处理。

3.1 URL Parsing

The URL parsing functions focus on splitting a URL string into its components, or on combining URL components into a URL string.

urllib.parse.urlparse(urlstring, scheme='', allow_fragments=True):url地址解析。

url地址通常标准格式如下:scheme://netloc/path;parameters?query#fragment详细说明介绍可见http.md的介绍。返回是6个元素组成的元组。

urllib.parse.parse_qs(qs, keep_blank_values=False, strict_parsing=False, encoding='utf-8', errors='replace'):解析url参数字符串。

3.2 URL Quoting

urllib.parse.urlencode(query, doseq=False, safe='', encoding=None, errors=None, quote_via=quote_plus):多参数元组拼接为百分比编码后的字符串。

4、写在后面

概念比较空洞,实践出真知。简单内容可以参见threading_douban.py

上一篇下一篇

猜你喜欢

热点阅读