Pythonのネットワーク爬虫類:utllibライブラリのurlopen()関数使用


Pythonのネットワーク爬虫類:utllibライブラリのurlopen()関数使用
utllibライブラリの概要:utllibライブラリはpythonに組み込まれている最も基本的なネットワークリクエストライブラリです.ブラウザの動作をシミュレートし、指定したサーバにリクエストを送信し、サーバから返されたデータを保存できます.
utllibライブラリのurlopen()関数
def urlopen(url, data=None, timeout=socket._GLOBAL_DEFAULT_TIMEOUT,
            *, cafile=None, capath=None, cadefault=False, context=None):
    '''Open the URL url, which can be either a string or a Request object.

    *data* must be an object specifying additional data to be sent to
    the server, or None if no such data is needed.  See Request for
    details.

    urllib.request module uses HTTP/1.1 and includes a "Connection:close"
    header in its HTTP requests.

    The optional *timeout* parameter specifies a timeout in seconds for
    blocking operations like the connection attempt (if not specified, the
    global default timeout setting will be used). This only works for HTTP,
    HTTPS and FTP connections.

    If *context* is specified, it must be a ssl.SSLContext instance describing
    the various SSL options. See HTTPSConnection for more details.

    The optional *cafile* and *capath* parameters specify a set of trusted CA
    certificates for HTTPS requests. cafile should point to a single file
    containing a bundle of CA certificates, whereas capath should point to a
    directory of hashed certificate files. More information can be found in
    ssl.SSLContext.load_verify_locations().

    The *cadefault* parameter is ignored.

    This function always returns an object which can work as a context
    manager and has methods such as

    * geturl() - return the URL of the resource retrieved, commonly used to
      determine if a redirect was followed

    * info() - return the meta-information of the page, such as headers, in the
      form of an email.message_from_string() instance (see Quick Reference to
      HTTP Headers)

    * getcode() - return the HTTP status code of the response.  Raises URLError
      on errors.

    For HTTP and HTTPS URLs, this function returns a http.client.HTTPResponse
    object slightly modified. In addition to the three new methods above, the
    msg attribute contains the same information as the reason attribute ---
    the reason phrase returned by the server --- instead of the response
    headers as it is specified in the documentation for HTTPResponse.

    For FTP, file, and data URLs and requests explicitly handled by legacy
    URLopener and FancyURLopener classes, this function returns a
    urllib.response.addinfourl object.

    Note that None may be returned if no handler handles the request (though
    the default installed global OpenerDirector uses UnknownHandler to ensure
    this never happens).

    In addition, if proxy settings are detected (for example, when a *_proxy
    environment variable like http_proxy is set), ProxyHandler is default
    installed and makes sure the requests are handled through the proxy.

    '''

パラメータ説明:1、url:要求するurl 2、data:要求するdata.このパラメータ設定後、要求方法はPOST要求3、戻り値:戻り値はhttpである.client.HTTPResponseオブジェクト.このオブジェクトはクラスファイルハンドルオブジェクトです.read(size)、readline()、readline()、getcode()などの方法を使用できます.
例:
from urllib import request
resp=request.urlopen('http://www.baidu.com')
# print(resp.read())
# print('='*60)
# #     
# print(resp.readline())
# print('='*60)
#    ,      ,         
print(resp.readlines())
print('='*60)
#     
print(resp.getcode())