zoukankan      html  css  js  c++  java
  • python urllib.request

    一、简介

    urllib.request 模块提供了访问 URL 的相关功能

    二、常用函数

    urlopen

    urllib.request.urlopen("http://httpbin.org", timeout=1)
      // 访问网页,并设置1秒的超时时间(urlopen 只能实现最基本的请求)

    读:

    • .read()   // 读取网页(二进制)
    • .decode('utf-8')   // 以 utf-8 解码网页
    • .geturl()   // 获取访问的 URL

    信息:

    • .info()   // 获取网响应页的 Headers 信息
    • .headers   // 获取网页响应的 Headers 信息
    • .getheaders()   // 获取网页响应的 Headers 信息(以列表形式返回)
    • .getheader(name="Content-Type")   // 获取网页响应的 Headers 信息(查看指定的属性)
    • .version   // 查看 HTTP 使用的版本协议号

    状态码:

    • .getcode()   // 获取当前访问的状态码
    • .status   // 获取当前访问的状态码
    • .reason   // 获取当前访问的状态码(如访问成功则返回 OK)
    Request

    urllib.request.Request(url=url, data=data, headers=header, method="POST")

    • .add_header   // 添加新的 Header(接受元组类型)
    参数 作用
    url 需请求的 url
    data 必须为 bytes(字节流)类型,如为字典,可用 urllib.parse.urlencode()
    headers 请求头
    origin_req_host 指定请求方的 host 名称或 ip 地址
    unverifiable 设置网页是否需要验证(默认为 Flase)
    method 指定请求方法(如:GET、POST等)

    三、实例

    1、读取网页,并以 utf-8 格式解码
    #  读取网页,并以 utf-8 格式解码
    urllib.request.urlopen("http://httpbin.org").read().decode('utf-8')
    
    2、获取访问的 URL
    #  获取访问的 URL
    urllib.request.urlopen("http://httpbin.org").geturl()
    
    3、获取 Headers 信息
    #  获取 Headers 信息
    urllib.request.urlopen("http://httpbin.org").info()
    
    4、获取访问的状态码
    #  获取访问的状态码
    urllib.request.urlopen("http://httpbin.org").getcode()
    
    5、指定 Headers 访问网页
    import urllib.request
    
    #  指定访问的 URL
    url = "http://httpbin.org/get"
    
    #  指定访问的 Headers
    header = {
        "Host": "httpbin.org",
        "Referer": "http://httpbin.org/",
        "User-Agent": "Mozilla/5.0 (Windows NT 99.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.83 Safari/537.36",
    }
    
    #  使用指定的 Headers 访问网页
    test = urllib.request.Request(url=url, headers=header)
    
    #  以 utf-8 的格式打印出访问的页面
    test_2 = urllib.request.urlopen(test).read().decode("utf-8")
    print (test_2)
    
    6、发送带参数的 GET 请求
    import urllib.request
    import urllib.parse
    
    #  指定访问的 URL
    url = "http://httpbin.org/get"
    
    #  指定访问的 Headers
    header = {
        "Host": "httpbin.org",
        "Referer": "http://httpbin.org/",
        "User-Agent": "Mozilla/5.0 (Windows NT 99.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.83 Safari/537.36",
    }
    
    #  设置需传递的参数(使用 urlencode 将字典转换成可提交的参数,如:a=test_1&b=test_2)
    data = urllib.parse.urlencode({'a':'test_1', 'b':'test_2'})
    url = url + "?" + data
    
    #  使用指定的 Headers 访问网页
    test = urllib.request.Request(url=url, headers=header)
    
    #  以 utf-8 的格式打印出访问的页面
    test_2 = urllib.request.urlopen(test).read().decode("utf-8")
    print (test_2)
    
    7、发送带参数的 POST 请求
    import urllib.request
    import urllib.parse
    
    #  指定访问的 URL
    url = "http://httpbin.org/post"
    
    #  指定访问的 Headers
    header = {
        "Host": "httpbin.org",
        "Origin": "http://httpbin.org",
        "Referer": "http://httpbin.org/",
        "User-Agent": "Mozilla/5.0 (Windows NT 99.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.83 Safari/537.36",
    }
    
    #  设置需传递的参数(使用 urlencode 将字典转换成可提交的参数,如:a=test_1&b=test_2)
    data = urllib.parse.urlencode({'a':'test_1', 'b':'test_2'})
    #  将序列化后的字符串转换成二进制数据(POST 请求携带的参数是二进制)
    data = bytes(data, encoding='utf-8')
    
    #  使用指定的 Headers 访问网页
    test = urllib.request.Request(url=url, headers=header, data = data, method="POST")
    #  指定新的 Headers(接受元组类型(会替换掉对应的项))
    test.add_header("User-Agent", "Mozilla/99.0 (Windows NT 99.0; Win99; x99) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.83 Safari/537.36",
    )
    #  以 utf-8 的格式打印出访问的页面
    test_2 = urllib.request.urlopen(test).read().decode("utf-8")
    print (test_2)
    
    8、设置全局代理
    import urllib.request
    
    #  需访问测试页面
    url = "http://httpbin.org/ip"
    
    #  设置代理 IP
    ip = {"http":"127.0.0.1:8888"}
    proxy_ip = urllib.request.ProxyHandler(ip)
    
    #  使用 build_opener() 构建一个 opener 对象
    opener = urllib.request.build_opener(proxy_ip)
    #  设置新的 Headers
    header = ('User-Agent','Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.97 Safari/537.36')
    opener.addheaders = [header]
    urllib.request.install_opener(opener)
    
    #  访问测试的页面
    response = urllib.request.urlopen(url)
    #  以 utf-8 的格式打印出访问的页面
    html = response.read().decode('utf-8')
    print (html)
    
  • 相关阅读:
    MyEclipse配置输出控制台信息至文本文件里
    IOS开发证书显示“此证书的签发者无效”解决方法
    leetcode之Find All Numbers Disappeared in an Array
    自己定义控件三部曲视图篇(二)——FlowLayout自适应容器实现
    浅谈BloomFilter【上】基本概念和实现原理
    mybatis 常用的jabcType与javaType对应
    springboot WebMvcConfigurerAdapter替代
    动态insert mybatis与ibatis
    动态set mybatis与ibatis的写法
    springboot 配置mybatis打印sql
  • 原文地址:https://www.cnblogs.com/anonymous-test/p/13607080.html
Copyright © 2011-2022 走看看