• requests库简单介绍与使用


    一、安装

    pip3 install requests

    二、作用

      和urllib是同一个位置的,模拟浏览器发送请求,比urllib操作起来更为简单,比如无需手动为url添加查询字符,也不需要对post数据进行表单编码等。

    三、使用

    1、发送get请求

    (1)响应对象

    a、r.text  字符串形式查看响应内容

    b、r.content  字节类型查看响应内容

    c、r.encoding  查看或者设置编码类型

    d、r.status_code  查看状态码

    e、r.headers  查看头部

    f、r.url  查看所请求的url

    g、r.json()  查看json格式的数据

    (2)代码演示

    #!/usr/local/bin/python3.7
    
    import requests
    
    # url
    url = 'https://www.baidu.com'
    # 请求头
    headers = {
         'User-Agent':'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.163 Safari/537.36'
         }
    r = requests.get(url, headers=headers)
    # 将编码格式转换为utf-8
    r.encoding = 'utf-8'
    print(r.text)
    
    # 带参数的get请求
    url = 'https://www.baidu.com/s'
    data = {
        'ie':'utf-8',
        'wd':'中国',
    }
    # 发送请求
    r = requests.get(url=url, headers=headers, params=data)
    r.encoding='utf-8'
    print(r.text)

    2、发送post请求

    #!/usr/local/bin/python3.7
    
    import requests
    import ssl
    
    # 请求url
    url = 'https://cn.bing.com/tlookupv3?isVertical=1&&IG=1E1AE90B09BB41E28506E0ADC9E45704&IID=translator.5028.2'
    # 请求头
    headers = {
         'User-Agent':'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.163 Safari/537.36'
         }
    # 请求formdata
    formdata = {
        'from': 'en',
        'to': 'zh-Hans',
        'text': 'cat'
    }
    # 发送请求
    r = requests.post(url, headers=headers, data=formdata)
    print(r.json())

    3、代理

    #!/usr/local/bin/python3.7
    
    import requests
    
    # url
    url = 'https://www.baidu.com/s?ie=utf-8&wd=ip'
    # 请求头
    headers = {
         'User-Agent':'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.163 Safari/537.36'
         }
    # 设置代理
    proxies = {
        'http':'http://171.43.258.176:30609'
    }
    # 发送请求
    r = requests.get(url, headers=headers, proxies=proxies)
    with open('Reptile/daili.html','wb') as fp:
        fp.write(r.content)

    6、cookie

    #!/usr/local/bin/python3.7
    import requests
    
    # 创建session会话
    s = requests.Session()
    # 人人网登录演示
    post_url = 'http://www.renren.com/ajaxLogin/login?1=1&uniqueTimestamp=202053842806'
    headers = {
        'User-Agent':'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_4) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/13.1 Safari/605.1.15'
        } 
    # 请求参数
    formdata = {
        'email':'1882744xxxx',
        'icode':'',    
        'origURL':'http://www.renren.com/home',
        'domain':'renren.com',
        'key_id':'1',
        'captcha_type':'web_login',
        'password':'32904758c29a901f87064f3aa151f9c373112ed66370fe9b28152f0ce63d3796',
        'rkey':'d6b3acc434f19c92fad1f33176e506f0',
        'f':'https%3A%2F%2Fbaidu.com%2F'
    }
    # 发送请求
    r = s.post(post_url, headers=headers, data=formdata)
    # print(r.text)
    
    # 登录成功后访问个人中心页面
    get_url = 'http://www.renren.com/974549125/profile'
    # 发送get请求
    rg = s.get(get_url, headers=headers)
    print(rg.text)
  • 相关阅读:
    angularjs学习笔记一之显示数据,修改数据
    收藏/不再提醒
    CSS3动画
    Content-Type
    键盘快捷键
    url、href、src 详解
    关于docnment.write() 会清空原来的内容
    jq事件注意点
    echart的自适应
    键盘事件
  • 原文地址:https://www.cnblogs.com/lxmtx/p/13029537.html
Copyright © 2020-2023  润新知