• Python Urllib库详解


    Urllib库详解

    什么是Urllib?

    Python内置的HTTP请求库

    urllib.request 请求模块
    urllib.error 异常处理模块
    urllib.parse url解析模块
    urllib.robotparser robots.txt解析模块

    相比Python2变化

    python2

    import urllib2
    response = urllib2.urlopen('http://www.baidu.com')
    

    python3

    import urllib.request
    response = urllib.request.urlopen('http://www.baidu.com')
    

    urllib

    urlopen

    urllib.request.urlopen(url,data=None,[timeout,]*,cafile=None,capath=None,cadefault=False,context=None)
    
    import urllib.request
    response = urllib.request.urlopen('http://www.baidu.com')
    print(response.read().decode('utf-8'))
    
    import urllib.parse
    import urllib.request
    
    data = bytes(urllib.parse.urlencode({'word':'hello'}),encoding='utf8')
    response = urllib.request.urlopen('http://httpbin.org/post',data=data)
    print(response.read())
    
    import urllib.request
    response = urllib.request.urlopen('http://httpbin.org/get',timeout=1)
    print(response.read())
    
    import socket
    import urllib.request
    import urllib.error
    try:
        response = urllib.request.urlopen('http://httpbin.org/get',timeout=0.1)
    except urllib.error.URLError as e:
        if isinstance(e.reason,socket.timeout):
            print('TIME OUT')
    

    响应

    响应类型

    import urllib.request
    response = urllib.request.urlopen('https://www.python.org')
    print(type(response))
    

    状态码、响应头

    import urllib.request
    response = urllib.request.urlopen('http://www.python.org')
    print(response.status)
    print(response.getheaders())
    

    Request

    import urllib.request
    request = urllib.request.Request('https://python.org')
    response = urllib.request.urlopen(request)
    print(response.read().decode('utf-8'))
    
    from urllib import request,parse
    url = 'http://httpbin.org/post'
    headers = {
        'User-Agent':'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36',
        'Host':'httpbin.org'
    }
    dict = {
        'name':'puqunzhu'
    }
    data = bytes(parse.urlencode(dict),encoding='utf8')
    req = request.Request(url=url,data=data,headers=headers,method='POST')
    response = request.urlopen(req)
    print(response.read().decode('utf-8'))
    
    from urllib import request,parse
    url = 'http://httpbin.org/post'
    dict = {
        'name':'puqunzhu'
    }
    data = bytes(parse.urlencode(dict),encoding='utf8')
    req = request.Request(url=url,data=data,method='POST')
    req.add_header('User-Agent','Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36')
    response = request.urlopen(req)
    print(response.read().decode('utf-8'))
    

    Handler

    代理

    import urllib.request
    proxy_handler = urllib.request.ProxyHandler({
        'http':'http://61.135.217.7:80',
        'https':'https://61.150.96.27:46111',
    })
    opener = urllib.request.build_opener(proxy_handler)
    response = opener.open('http://www.baidu.com')
    print(response.read())
    
    import http.cookiejar,urllib.request
    cookie = http.cookiejar.CookieJar()
    handler = urllib.request.HTTPCookieProcessor(cookie)
    opener = urllib.request.build_opener(handler)
    response = opener.open('http://www.baidu.com')
    for item in cookie:
        print(item.name+"="+item.value)
    
    import http.cookiejar,urllib.request
    filename = "cookie.txt"
    cookie = http.cookiejar.MozillaCookieJar(filename)
    handler = urllib.request.HTTPCookieProcessor(cookie)
    opener = urllib.request.build_opener(handler)
    response = opener.open('http://www.baidu.com')
    cookie.save(ignore_discard=True,ignore_expires=True)
    
    import http.cookiejar,urllib.request
    filename = "cookie.txt"
    cookie = http.cookiejar.LWPCookieJar(filename)
    handler = urllib.request.HTTPCookieProcessor(cookie)
    opener = urllib.request.build_opener(handler)
    response = opener.open("http://www.baidu.com")
    cookie.save(ignore_discard=True,ignore_expires=True)
    
    import http.cookiejar,urllib.request
    cookie = http.cookiejar.LWPCookieJar()
    cookie.load('cookie.txt',ignore_discard=True,ignore_expires=True)
    handler = urllib.request.HTTPCookieProcessor(cookie)
    opener = urllib.request.build_opener(handler)
    response = opener.open("http://www.baidu.com")
    print(response.read().decode('utf-8'))
    

    异常处理

    from urllib import request,error
    try:
        response = request.urlopen('http://cuiqingcai.com/index.htm')
    except error.URLError as e:
        print(e.reason)
    
    from urllib import request,error
    try:
        response = request.urlopen('http://cuiqingcai.com/index.htm')
    except error.HTTPError as e:
        print(e.reason,e.code,e.headers,sep="\n")
    except error.URLError as e:
        print(e.reason)
    else:
        print("Request Successfully")
    
    import socket
    import urllib.request
    import urllib.error
    try:
        response = urllib.request.urlopen('http://www.baiduc.com',timeout=0.01)
    except urllib.error.URLError as e:
        print(type(e.reason))
        if isinstance(e.reason,socket.timeout):
            print("TIME OUT")
    

    URL解析

    urlparse

    urllib.parse.urlparse(urlstring,scheme='',allow_fragments=True)
    
    from urllib.parse import urlparse
    result = urlparse('http://www.baidu.com/index.html;urser?id=5#comment')
    print(type(result),result)
    
    from urllib.parse import urlparse
    result = urlparse('www.baidu.com/index.html;user?id=5#comment,scheme="https"')
    print(result)
    
    from urllib.parse import urlparse
    result = urlparse('http://www.baidu.com/index.html;user?id=5#comment,scheme="https"')
    print(result)
    
    from urllib.parse import urlparse
    result = urlparse('http://www.baidu.com/index.html;user?id=5#comment,allow_fragments=False')
    print(result)
    
    from urllib.parse import urlparse
    result = urlparse('http://www.baidu.com/index.html#comment',allow_fragments=False)
    print(result)
    

    utlunoarse

    from urllib.parse import urlunparse
    data =['http','www.baidu.com','index.html','user','a=6','comment']
    print(urlunparse(data))
    

    urljoin

    from urllib.parse import urljoin
    print(urljoin('http://www.baidu.com','?category=2#comment'))
    print(urljoin('htttp://www.baidu.com/about.html','htttp://www.baidu.com/FAQ.html'))
    

    urlencode

    from urllib.parse import urlencode
    params = {
        'name':'puqunzhu',
        'age':23
    }
    base_url="http://www.baidu.com?"
    url=base_url + urlencode(params)
    print(url)
    
    大道理谁都懂,鸡汤也听过,可我们为什么还是过不好这一生。
  • 相关阅读:
    ES6中关于数据类型的拓展:Symbol类型
    ES里关于数组的拓展
    ES里关于对象的拓展
    ES6里关于函数的拓展(三)
    ES6里关于函数的拓展(二)
    ES6里关于函数的拓展(一)
    ES6里关于正则表达式的拓展
    ES6里关于模板字面量的拓展
    Android之怎样实现滑动页面切换【Fragment】
    java quartz的使用,做时间轮询调用 CronTrigger
  • 原文地址:https://www.cnblogs.com/puqunzhu/p/9803769.html
Copyright © 2020-2023  润新知