zoukankan      html  css  js  c++  java
  • requests 介绍

    一.  requests 参数 

    - method:  提交方式
                - url:     提交地址
                - params:  在URL中传递的参数,GET 
                - data:    在请求体里传递的数据
                - json     在请求体里传递的数据
                - headers  请求头
                - cookies  Cookies
                - files    上传文件
                - auth     基本认知(headers中加入加密的用户名和密码)
                - timeout  请求和响应的超市时间
                - allow_redirects  是否允许重定向
                - proxies  代理
                - verify   是否忽略证书
                - cert     证书文件
                - stream   村长下大片
                - session: 用于保存客户端历史访问信息

    a. url

    1. 分析url
        - https://www.baidu.com/s?wd=美女 = https://www.baidu.com/s?wd=%E7%BE%8E%E5%A5%B3
        - 导入from urllib.parse import urlencode
    2. 分析请求头headers
        - User-Agent
    3. Get请求不需要考虑请求体
    
    
    import requests
    
    from urllib.parse import urlencode
    
    search_input = input(">>: ")
    keyword = urlencode({"wd":search_input},encoding="utf-8")
    url = "url = https://www.baidu.com/s?" + keyword
    
    requests.get(url,
                 headers={
                    "User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.146 Safari/537.36"
                 }
                 )
    View Code

    b. params

    # params 参数省去自己转换
    
    
    import requests
    
    from urllib.parse import urlencode
    
    search_input = input(">>: ")
    
    
    requests.get("https://www.baidu.com/s?",
                 params={"wd":search_input},
                 headers={
                    "User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.146 Safari/537.36"
                 }
                 )
    View Code

    c. headers 

    ddd
    View Code

    d. cookies

    111
    View Code 

    e. file 发送文件

    import requests
    
    requests.post(
        url='xxx',
        filter={
            'name1': open('a.txt','rb'),   #名称对应的文件对象
            'name2': ('bbb.txt',open('b.txt','rb'))     #表示上传到服务端的名称为 bbb.txt
        }
    )
    View Code

    f. auth 认证

    #配置路由器访问192.168.0.1会弹出小弹窗,输入用户名,密码 点击登录不是form表单提交,是基本登录框,这种框会把输入的用户名和密码 经过加密放在请求头发送过去
    
    import requests
    
    requests.post(
        url='xxx',
        filter={
            'name1': open('a.txt','rb'),   #名称对应的文件对象
            'name2': ('bbb.txt',open('b.txt','rb'))     #表示上传到服务端的名称为 bbb.txt
        }
    )
    View Code

    g. stream 流

    #如果服务器文件过大,循环下载
    
    def param_stream():
        ret = requests.get('http://127.0.0.1:8000/test/', stream=True)
        print(ret.content)
        ret.close()
    
        # from contextlib import closing
        # with closing(requests.get('http://httpbin.org/get', stream=True)) as r:
        # # 在此处理响应。
        # for i in r.iter_content():
        # print(i)
    View Code

    h. session  和django不同  事例:简化抽屉点赞

        import requests
    
        session = requests.Session()
    
        ### 1、首先登陆任何页面,获取cookie
    
        i1 = session.get(url="http://dig.chouti.com/help/service")
    
        ### 2、用户登陆,携带上一次的cookie,后台对cookie中的 gpsd 进行授权
        i2 = session.post(
            url="http://dig.chouti.com/login",
            data={
                'phone': "8615131255089",
                'password': "xxxxxx",
                'oneMonth': ""
            }
        )
    
        i3 = session.post(
            url="http://dig.chouti.com/link/vote?linksId=8589623",
        )
        print(i3.text)
    View Code

    二. 登录事例

    a. 查找汽车之家新闻 标题 链接 图片写入本地

    View Code

    b. 抽屉点赞 获取页面和登录都会获取gpsd  点赞会使用获取页面的gpsd 而不是登录的gpsd

    View Code

    c. 登录github 携带cookie登录

    View Code

    d. 登录github 演示

    一. 先获取页面
    
    	1.请求的url: https://github.com/login
    	2.请求方法: GET
    	3.请求头分析
    		- 清除cookie,发现请求头里没有cookie,所以不要写
    		- User-Aget
    	4.服务端可能相应头带点东西
    		- 例如规定提交表单时必须携带cookie
    		- 有可能发送客户端一个csrftoken编码,第二次提交时必须携带csrftoken
    
    二. 提交表单完成请求
    	1.请求url: https://github.com/session
    	2.请求方法: POST
    	3.请求头分析
    		- Referer: https://github.com/
    		- User-Agent: "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.146 Safari/537.36"
    		- cookies
    	4.请求体
    		commit: Sign in
    		utf8: ✓
    		authenticity_token: HIHg6UxhecIFqAGrHiGapG4y+8OShE6xJhJHU6wxg3mTo30JhYiENFu+JWtdFSj//5cCx6NaMEgcnsvF5MA8VQ==
    		#分析authenticity_token数据,只可能是第一次访问,相应头或相应体发过来的数据
    		login: 877252373@qq.com
    		password: 123456
    	5.分析第一次请求获取到的authenticity_token
    		- 在相应头中找不到
    		- 在相应体中找
    	6.结果为最后的跳转结果
    import requests
    import re
    
    # 第一次请求
    
    response_get = requests.get("https://github.com/login",
                            headers={
                                "User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.146 Safari/537.36"
                            })
    
    authenticity_token = re.findall(r"authenticity_token.*?value="(.*?)"",response_get.text,re.S)[0]
    response_cookie = response_get.cookies.get_dict()
    
    # 第二次登录
    
    response_post = requests.post("https://github.com/session",
                headers = {
                    "User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.146 Safari/537.36",
                    "Referer":"https://github.com/",
                },
                cookies = response_cookie,
                data={
                    "commit": "Sign in",
                    "utf8": "",
                    "authenticity_token": authenticity_token,
                    "login": "8772 @ qq.com",
                    "password": "123"
                })
    
    print(response_post.text)

      

      

     

      

     

    沛齐

  • 相关阅读:
    classic problem: select sortjava
    【转】排序算法复习(Java实现)(二): 归并排序,堆排序,桶式排序,基数排序
    【转】排序算法复习(Java实现)(一): 插入,冒泡,选择,Shell,快速排序
    classic problem: 100 horse and 100 dan
    good chocolate
    【转】Java内存模型 http://blog.csdn.net/silentbalanceyh
    http header/ eclipse package path
    design patterns: factory and abstractFactory
    阅读笔记
    提取Dump文件的Callstack,创建windbg的一个扩展应用
  • 原文地址:https://www.cnblogs.com/golangav/p/7445203.html
Copyright © 2011-2022 走看看