• python接口自动化3-自动发帖(session)


    前言

    上一篇模拟登录博客园,但这只是第一步,一般登录后,还会有其它的操作,如发帖,评论等,这时候如何保持会话呢?

     (由于博客园最近登录机制变了,登录全部走cookie登录)

    一、session简介

    1.查看帮助文档,贴了一部分,后面省略了

    >>import requests
    >>help(requests.session())

    class Session(SessionRedirectMixin)
     |  A Requests session.
     |  
     |  Provides cookie persistence, connection-pooling, and configuration.
     |  
     |  Basic Usage::
     |  
     |    >>> import requests
     |    >>> s = requests.Session()
     |    >>> s.get('http://httpbin.org/get')
     |    <Response [200]>
     |  
     |  Or as a context manager::
     |  
     |    >>> with requests.Session() as s:
     |    >>>     s.get('http://httpbin.org/get')
     |    <Response [200]>

     

    二、使用session登录

    1.使用session登录只需在上一篇基础上稍做修改

    # coding:utf-8
    import requests
    url = "https://passport.cnblogs.com/user/signin"

    headers = {
                      头部信息已省略

                       }
    payload = {"input1":"xxx",
                    "input2":"xxx",
                    "remember":True}

    # r = requests.post(url, json=payload, headers=headers,verify=False)

    # 修改后如下
    s = requests.session()
    r = s.post(url, json=payload, headers=headers,verify=False)
    print r.json()

    三、保存编辑

    1.先打开我的随笔,手动输入内容后,打开fiddler抓包

    2.把body的参数内容写成字典格式,有几个空的参数不是必填的,可以去掉

    body = {"__VIEWSTATE": "",
            "__VIEWSTATEGENERATOR":"FE27D343",
            "Editor$Edit$txbTitle":"这是我的标题:上海-悠悠",
            "Editor$Edit$EditorBody":"<p>这里是中文内容:http://www.cnblogs.com/yoyoketang/</p>",
            "Editor$Edit$Advanced$ckbPublished":"on",
            "Editor$Edit$Advanced$chkDisplayHomePage":"on",
            "Editor$Edit$Advanced$chkComments":"on",
            "Editor$Edit$Advanced$chkMainSyndication":"on",
            "Editor$Edit$lkbDraft":"存为草稿",
             }

    3.用上面的session继续发送post请求

    4.执行后,查看我的草稿箱就多了一条新增的了

     

    四、参考代码

     # coding:utf-8
    import requests

    # 先打开登录首页,获取部分cookie
    url = "https://passport.cnblogs.com/user/signin"
    headers = {
                "User-Agent": "Mozilla/5.0 (Windows NT 10.0; WOW64; rv:44.0) Gecko/20100101 Firefox/44.0"
               }  

    s = requests.session()
    r = s.get(url, headers=headers,verify=False)
    print s.cookies

    # 添加登录需要的两个cookie
    c = requests.cookies.RequestsCookieJar()

    c.set('.CNBlogsCookie', '这里是抓到的')  # 填上面抓包内容
    c.set('.Cnblogs.AspNetCore.Cookies','这里是抓到的')  # 填上面抓包内容
    c.set('AlwaysCreateItemsAsActive',"True")
    c.set('AdminCookieAlwaysExpandAdvanced',"True")
    s.cookies.update(c)
    print s.cookies

    # 登录成功后保存编辑内容
    r1 = s.get("https://i.cnblogs.com/EditPosts.aspx?opt=1", headers=headers, verify=False)

    # 保存草稿箱
    url2= "https://i.cnblogs.com/EditPosts.aspx?opt=1"
    body = {"__VIEWSTATE": "",
            "__VIEWSTATEGENERATOR":"FE27D343",
            "Editor$Edit$txbTitle":"这是3111",
            "Editor$Edit$EditorBody":"<p>这里111:http://www.cnblogs.com/yoyoketang/</p>",
            "Editor$Edit$Advanced$ckbPublished":"on",
            "Editor$Edit$Advanced$chkDisplayHomePage":"on",
            "Editor$Edit$Advanced$chkComments":"on",
            "Editor$Edit$Advanced$chkMainSyndication":"on",
            "Editor$Edit$Advanced$txbEntryName":"",
            "Editor$Edit$Advanced$txbExcerpt":"",
            "Editor$Edit$Advanced$tbEnryPassword":"",
            "Editor$Edit$lkbDraft":"存为草稿",
             }
    r2 = s.post(url2, data=body, verify=False)
    print r.content

  • 相关阅读:
    python 类 专有方法
    当请求进入Nginx后,每个HTTP执行阶段的作用
    jquery 监听不起效果的小问题汇总
    shell 脚本中 while 只执行一次
    LVS (Linux虚拟服务器)模型及算法
    TCP 通信时序及状态变迁
    Golang 谷歌搜索api 实现搜索引擎(前端 bootstrap + jquery)
    Golang 简单 http 代理转发
    Golang 简单静态web服务器
    Golang TCP转发到指定地址
  • 原文地址:https://www.cnblogs.com/liunaixu/p/11077823.html
Copyright © 2020-2023  润新知