zoukankan      html  css  js  c++  java
  • 【转】python3 urllib.request 网络请求操作

    python3 urllib.request 网络请求操作

    基本的网络请求示例

    复制代码
    '''
    Created on 2014年4月22日
    
    @author: dev.keke@gmail.com
    '''
    import urllib.request
    
    #请求百度网页
    resu = urllib.request.urlopen('http://www.baidu.com', data = None, timeout = 10)
    print(resu.read(300))
    
    #指定编码请求
    with urllib.request.urlopen('http://www.baidu.com') as resu:
        print(resu.read(300).decode('GBK'))
        
    #指定编码请求
    f = urllib.request.urlopen('http://www.baidu.com')
    print(f.read(100).decode('utf-8'))
    复制代码

     发送数据请求,CGI程序处理

    复制代码
    >>> import urllib.request
    >>> req = urllib.request.Request(url='https://localhost/cgi-bin/test.cgi',
    ...                       data=b'This data is passed to stdin of the CGI')
    >>> f = urllib.request.urlopen(req)
    >>> print(f.read().decode('utf-8'))
    Got Data: "This data is passed to stdin of the CGI"
    复制代码

    PUT请求

    复制代码
    import urllib.request
    DATA=b'some data'
    req = urllib.request.Request(url='http://localhost:8080', data=DATA,method='PUT')
    f = urllib.request.urlopen(req)
    print(f.status)
    print(f.reason)
    复制代码

    基本的HTTP验证,登录请求

    复制代码
    import urllib.request
    # Create an OpenerDirector with support for Basic HTTP Authentication...
    auth_handler = urllib.request.HTTPBasicAuthHandler()
    auth_handler.add_password(realm='PDQ Application',
                              uri='https://mahler:8092/site-updates.py',
                              user='klem',
                              passwd='kadidd!ehopper')
    opener = urllib.request.build_opener(auth_handler)
    # ...and install it globally so it can be used with urlopen.
    urllib.request.install_opener(opener)
    urllib.request.urlopen('http://www.example.com/login.html')
    复制代码

    支持代理方式验证请求

    复制代码
    proxy_handler = urllib.request.ProxyHandler({'http': 'http://www.example.com:3128/'})
    proxy_auth_handler = urllib.request.ProxyBasicAuthHandler()
    proxy_auth_handler.add_password('realm', 'host', 'username', 'password')
    
    opener = urllib.request.build_opener(proxy_handler, proxy_auth_handler)
    # This time, rather than install the OpenerDirector, we use it directly:
    opener.open('http://www.example.com/login.html')
    复制代码

    添加 http headers

    import urllib.request
    req = urllib.request.Request('http://www.example.com/')
    req.add_header('Referer', 'http://www.python.org/')
    r = urllib.request.urlopen(req)

    添加 user-agent

    import urllib.request
    opener = urllib.request.build_opener()
    opener.addheaders = [('User-agent', 'Mozilla/5.0')]
    opener.open('http://www.example.com/')

    带参数的GET 请求

    >>> import urllib.request
    >>> import urllib.parse
    >>> params = urllib.parse.urlencode({'spam': 1, 'eggs': 2, 'bacon': 0})
    >>> f = urllib.request.urlopen("http://www.musi-cal.com/cgi-bin/query?%s" % params)
    >>> print(f.read().decode('utf-8'))

    带参数的POST请求

    复制代码
    >>> import urllib.request
    >>> import urllib.parse
    >>> data = urllib.parse.urlencode({'spam': 1, 'eggs': 2, 'bacon': 0})
    >>> data = data.encode('utf-8')
    >>> request = urllib.request.Request("http://requestb.in/xrbl82xr")
    >>> # adding charset parameter to the Content-Type header.
    >>> request.add_header("Content-Type","application/x-www-form-urlencoded;charset=utf-8")
    >>> f = urllib.request.urlopen(request, data)
    >>> print(f.read().decode('utf-8'))
    复制代码

    指定代理方式请求

    >>> import urllib.request
    >>> proxies = {'http': 'http://proxy.example.com:8080/'}
    >>> opener = urllib.request.FancyURLopener(proxies)
    >>> f = opener.open("http://www.python.org")
    >>> f.read().decode('utf-8')

    无添加代理

    >>> import urllib.request
    >>> opener = urllib.request.FancyURLopener({})
    >>> f = opener.open("http://www.python.org/")
    >>> f.read().decode('utf-8')

    http://www.cnblogs.com/cocoajin/p/3679821.html

  • 相关阅读:
    用户和用户组管理
    浏览器从输入URL到页面加载显示完成全过程解析
    easyui form提交和formdata提交记录,查看FormData对象内部的值
    Hybrid App中原生页面 VS H5页面(分享)
    ui-router .state参数配置
    利用webpack手动构建vue工程
    使用json对象要注意的地方
    js中使用0 “” null undefined {}需要注意
    已知宽高和未知宽高的div块的水平垂直居中
    jquery easyui datagrid 空白条处理 自适应宽高 格式化函数formmater 初始化时会报错 cannot read property 'width'||'length' of null|undefined
  • 原文地址:https://www.cnblogs.com/themost/p/6837652.html
Copyright © 2011-2022 走看看