美文网首页
python3.6 urllib和requests简单用法

python3.6 urllib和requests简单用法

作者: dongshixiao | 来源:发表于2018-03-23 07:37 被阅读0次

    python3.6 urllib简单用法,主要是想和requests做对比

    1.urllib(自带)

    from urllib import request
    import urllib
    import json
    
    URL_IP = 'http://httpbin.org/ip'
    URL_GET = 'http://httpbin.org/get'
    
    # GET 简单请求一个获取ip的url
    def simple_ip_request():
        response = request.urlopen(URL_IP)
        print("返回请求:")
        print(response.info())
        print(json.loads(response.read()))
    
    # GET 带参数
    def simple_get_request():
        # 参数
        params = urllib.parse.urlencode({'param1': 'hello', 'param2': 'world'})
        response = request.urlopen('?'.join([URL_GET, "%s"]) % params)
        print(response.info())
        print(response.getcode())
        print(json.loads(response.read()))
    
    # if __name__=='__main__':
    simple_ip_request()
    simple_get_request()
    

    OUT:

    返回请求:
    Connection: close
    Server: meinheld/0.6.1
    Date: Mon, 22 Jan 2018 06:52:35 GMT
    Content-Type: application/json
    Access-Control-Allow-Origin: *
    Access-Control-Allow-Credentials: true
    X-Powered-By: Flask
    X-Processed-Time: 0.000494003295898
    Content-Length: 32
    Via: 1.1 vegur
    
    {'origin': '49.80.163.208'}
    Connection: close
    Server: meinheld/0.6.1
    Date: Mon, 22 Jan 2018 06:52:35 GMT
    Content-Type: application/json
    Access-Control-Allow-Origin: *
    Access-Control-Allow-Credentials: true
    X-Powered-By: Flask
    X-Processed-Time: 0.00118207931519
    Content-Length: 309
    Via: 1.1 vegur
    
    200
    {'args': {'param1': 'hello', 'param2': 'world'}, 'headers': {'Accept-Encoding': 'identity', 'Connection': 'close', 'Host': 'httpbin.org', 'User-Agent': 'Python-urllib/3.6'}, 'origin': '49.80.163.208', 'url': 'http://httpbin.org/get?param1=hello&param2=world'}
    
    Process finished with exit code 0
    

    2.requests(没有的话需要:pip install requests)

    import requests
    
    URL_IP = 'http://httpbin.org/ip'
    URL_GET = 'http://httpbin.org/get'
    
    # GET 简单请求一个获取ip的url
    def simple_ip_request():
        response = requests.get(URL_IP)
        print(response.headers)
        print(response.json())
    
    # GET 带参数
    def simple_get_request():
        # 参数
        params = {'param1': 'hello', 'param2': 'world'}
        response = requests.get(URL_GET,params=params)
        print(response.headers)
        print(response.json())
    
    # if __name__=='__main__':
    simple_ip_request()
    simple_get_request()
    

    OUT:

    {'Connection': 'keep-alive', 'Server': 'meinheld/0.6.1', 'Date': 'Mon, 22 Jan 2018 07:09:31 GMT', 'Content-Type': 'application/json', 'Access-Control-Allow-Origin': '*', 'Access-Control-Allow-Credentials': 'true', 'X-Powered-By': 'Flask', 'X-Processed-Time': '0.000584125518799', 'Content-Length': '32', 'Via': '1.1 vegur'}
    {'origin': '49.80.163.208'}
    {'Connection': 'keep-alive', 'Server': 'meinheld/0.6.1', 'Date': 'Mon, 22 Jan 2018 07:09:32 GMT', 'Content-Type': 'application/json', 'Access-Control-Allow-Origin': '*', 'Access-Control-Allow-Credentials': 'true', 'X-Powered-By': 'Flask', 'X-Processed-Time': '0.000766038894653', 'Content-Length': '341', 'Via': '1.1 vegur'}
    {'args': {'param1': 'hello', 'param2': 'world'}, 'headers': {'Accept': '*/*', 'Accept-Encoding': 'gzip, deflate', 'Connection': 'close', 'Host': 'httpbin.org', 'User-Agent': 'python-requests/2.18.4'}, 'origin': '49.80.163.208', 'url': 'http://httpbin.org/get?param1=hello&param2=world'}
    

    2.1 requests的异常

    import requests
    from requests import exceptions
    import json
    
    URL = 'https://api.github.com'
    
    def build_url(endpoint):
        return '/'.join([URL, endpoint])
    
    # GET 简单请求一个获取ip的url
    def simple_request():
        try:
            response = requests.get(build_url('user/emails'), timeout=1)
            response.raise_for_status()
        except exceptions.Timeout as e:
            print(e)
        except exceptions.HTTPError as e:
            print(e)
        else:
            print(response.text)
            print(response.status_code)
    
    simple_request()
    

    OUT:

    401 Client Error: Unauthorized for url: [https://api.github.com/user/emails](https://api.github.com/user/emails)
    

    2.2 requests 下载图片

    import requests
    URL = 'http://vpp5.com/ueditor/php/upload/image/20170522/1495439095647174.png'
    headers = {
        'User-Agent': 'Mozilla/5.0 (iPhone; CPU iPhone OS 9_1 like Mac OS X) AppleWebKit/601.1.46 (KHTML, like Gecko) Version/9.0 Mobile/13B143 Safari/601.1'
    }
    response = requests.get(URL, headers=headers, stream=True)
    with open('demo.jpg','wb') as f:
        for chuck in response.iter_content(128):
            f.write(chuck)
    

    相关文章

      网友评论

          本文标题:python3.6 urllib和requests简单用法

          本文链接:https://www.haomeiwen.com/subject/hfkaqftx.html