一、session简介

查看 requests.session() 帮助文档(只贴了一部分内容)

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登录

博客园登录实操:

# coding:utf-8
import requests
'''
https的请求相对于http安全级别高,需要验证SSL证书
import urllib3 使用这个方法就OK了
urllib3.disable_warnings() 忽略警告
'''
import urllib3
urllib3.disable_warnings() url = "https://passport.cnblogs.com/user/signin" headers = {
"Accept": "application/json, text/javascript, */*; q=0.01",
"Content-Type": "application/json; charset=utf-8", #json格式
"X-Requested-With": "XMLHttpRequest",
"Referer": "https://passport.cnblogs.com/user/signin",
"Accept-Language": "zh-CN",
"Accept-Encoding": "gzip, deflate",
"User-Agent": "Mozilla/5.0 (Windows NT 6.1; WOW64; Trident/7.0; rv:11.0) like Gecko",
"Connection": "Keep-Alive",
"Cookie": "_ga=GA1.2.2031405227.1553413697; _gid=GA1.2.1694843763.1553413697; "
"ASP.NET_SessionId=jw21lhtlzwfs14grozrtn5es; "
"SERVERID=4fea726178f35f0633c3d1a5c08ace19|1553430739|1553430738",
} payload = {
"input1":"k2bbCom4IU6eUoLehhL1l+uvFscRoUS5V9ZXmiiRnls"
"jS1fhvbTbj+sVg46vjJ6n3hm2kTVfx7O+dJh9+s7Fv"
"sWbNg1boYxn+mF2QdOoLBT6Zx4debvK3ieMaolFvCZH"
"gggaP+lvB1boSxMvfbKjjhB0R1anz72zyN1OUhfuitU=",
"input2":"iK6m5phf0al626Sfn/mKzAunzXlmaY65X5WX4hha67"
"cp1iS81fUmp5TwP6y3XZt7SRHStQ147xR/jMeAcjPnD"
"H5nhnQeDispR6ZAgmEd8bjInoc81tAKycOmlqBGNeCOj"
"PweXlcR8pREJhm7iSPPHqmN8GJ4c7GGc5C/eZc4Uks=",
"remember":True
} s = requests.session()
r = s.post(url, json=payload, headers=headers, verify=False)
print(r.json())

返回的结果:

Fiddler中的结果:

三、自动发文,保存草稿博客

1.先打开登录首页,刷新一下,fiddler抓包,获取部分cookie

代码:

# coding:utf-8
import requests
import urllib3
urllib3.disable_warnings()
# 先打开登录首页,刷新一下,fiddler抓包,获取部分cookie
url = "https://passport.cnblogs.com/user/signin"
headers = {
"Accept": "text/html, application/xhtml+xml, */*",
"User-Agent": "Mozilla/5.0 (Windows NT 6.1; WOW64; Trident/7.0; rv:11.0) like Gecko",
"Accept-Encoding": "gzip, deflate",
"Connection": "Keep-Alive",
}
s = requests.session()
r = s.get(url, headers=headers, verify=False)
print("第一次打印")
print(s.cookies)

  

2.添加登录需要的两个cookie

代码:

c = requests.cookies.RequestsCookieJar()

c.set('.CNBlogsCookie', '25D6E068A4434E2532E9F553728ACDE1F94F18E8079E07660FA1'
'68C361D87E1A59533A6BA991A7B14091F6609057C32CDE3E4BEDFD'
'DEEA6684C7E5F96A98B77C106640480FA5B3A78EC4EEC76E9E2D5F60082CE6') # 填登录后的抓包内容
c.set('.Cnblogs.AspNetCore.Cookies', 'CfDJ8JcopKY7yQlPr3eegllP76PlTM6nN56TphTAQZU5'
'OFZAKIG6zQQZsZpvJ4y7IBNq4M87KdPYiThIpRzGCp0qaG'
'Q6s30ZdqMvlqDRI0dM2k_jUprCUQ-6FZsKkco_lYEz1v9QQ'
'I_NIl85NQgwqXbjgOmErQIMWUOqhp9_zd1lVQEctfJ8Dt7aa'
'UtO-YSPZ8glHRGQuvXghskz3MOk1IdDdnbUsytc15vJXTKtY'
'-b7HmN2KbsMkwnnF2MSWHztAOYNU8AIuZXS3mX-UrG9PEi0'
'2UTfjkaJltqX4RwddTjTk2DI') # 填登录后的抓包内容
c.set('AlwaysCreateItemsAsActive', "True")
c.set('AdminCookieAlwaysExpandAdvanced', "True") s.cookies.update(c)
print("第二次打印")
print(s.cookies)

3.登录成功后访问编辑文章

r1 = s.get("https://i.cnblogs.com/EditPosts.aspx?opt=1", headers=headers, verify=False)

4.打开新随笔,输入内容后保存为草稿,用fiddler抓包

从博客园处可以查看到:

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

body = {"__VIEWSTATE": "",
"__VIEWSTATEGENERATOR": "FE27D343",
"Editor$Edit$Advanced$chkComments": "on",
"Editor$Edit$Advanced$chkDisplayHomePage": "on",
"Editor$Edit$Advanced$chkMainSyndication": "on",
"Editor$Edit$Advanced$ckbPublished": "on",
"Editor$Edit$APOptions$Advancedpanel1$cklCategories$2": "1426593",
"Editor$Edit$EditorBody": "<p>jjjjjjjjjjjjjjjjjjjjjjjjjj"
"jjjjjjjjjjjjjjjjjjjjjjjjjjjjjj"
"jjjjjjrrrrrrrrrrrrrrrrrrrrrrrrr"
"rrrrrrrrrrrrrrrrrr</p>",
"Editor$Edit$lkbDraft": "存为草稿",
"Editor$Edit$txbTitle": "testinggggggggg",
}

  

6.用上面的 session 继续发送 post 请求,对参数稍作修改

url2 = "https://i.cnblogs.com/EditPosts.aspx?opt=1"

body = {"__VIEWSTATE": "",
"__VIEWSTATEGENERATOR": "FE27D343",
"Editor$Edit$Advanced$chkComments": "on",
"Editor$Edit$Advanced$chkDisplayHomePage": "on",
"Editor$Edit$Advanced$chkMainSyndication": "on",
"Editor$Edit$Advanced$ckbPublished": "on",
"Editor$Edit$APOptions$Advancedpanel1$cklCategories$2": "1426593",
"Editor$Edit$EditorBody": "<p>222222222222jjjjjjjjjjjjjjjjjjjjjjjjjj"
"jjjjjjjjjjjjjjjjjjjjjjjjjjjjjj"
"jjjjjjrrrrrrrrrrrrrrrrrrrrrrrrr"
"rrrrrrrrrrrrrrrrrr</p>",
"Editor$Edit$lkbDraft": "存为草稿",
"Editor$Edit$txbTitle": "testinggggggggg2222222222",
}
r2 = s.post(url2, data=body, verify=False)
print("第三次打印")
print(r.content.decode("utf-8"))

  

7.执行后,查看我的博客,就新增了一条草稿内容

详细内容:

到这里整体的自动发文就完成了

四、全部代码参考

# coding:utf-8
import requests
import urllib3
urllib3.disable_warnings()
# 先打开登录首页,刷新一下,fiddler抓包,获取部分cookie
url = "https://passport.cnblogs.com/user/signin"
headers = {
"Accept": "text/html, application/xhtml+xml, */*",
"User-Agent": "Mozilla/5.0 (Windows NT 6.1; WOW64; Trident/7.0; rv:11.0) like Gecko",
"Accept-Encoding": "gzip, deflate",
"Connection": "Keep-Alive",
}
s = requests.session()
r = s.get(url, headers=headers, verify=False)
print("第一次打印")
print(s.cookies) # 添加登录需要的两个cookie
c = requests.cookies.RequestsCookieJar() c.set('.CNBlogsCookie', '25D6E068A4434E2532E9F553728ACDE1F94F18E8079E07660FA1'
'68C361D87E1A59533A6BA991A7B14091F6609057C32CDE3E4BEDFD'
'DEEA6684C7E5F96A98B77C106640480FA5B3A78EC4EEC76E9E2D5F60082CE6') # 填登录后的抓包内容
c.set('.Cnblogs.AspNetCore.Cookies', 'CfDJ8JcopKY7yQlPr3eegllP76PlTM6nN56TphTAQZU5'
'OFZAKIG6zQQZsZpvJ4y7IBNq4M87KdPYiThIpRzGCp0qaG'
'Q6s30ZdqMvlqDRI0dM2k_jUprCUQ-6FZsKkco_lYEz1v9QQ'
'I_NIl85NQgwqXbjgOmErQIMWUOqhp9_zd1lVQEctfJ8Dt7aa'
'UtO-YSPZ8glHRGQuvXghskz3MOk1IdDdnbUsytc15vJXTKtY'
'-b7HmN2KbsMkwnnF2MSWHztAOYNU8AIuZXS3mX-UrG9PEi0'
'2UTfjkaJltqX4RwddTjTk2DI') # 填登录后的抓包内容
c.set('AlwaysCreateItemsAsActive', "True")
c.set('AdminCookieAlwaysExpandAdvanced', "True") s.cookies.update(c)
print("第二次打印")
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$Advanced$chkComments": "on",
"Editor$Edit$Advanced$chkDisplayHomePage": "on",
"Editor$Edit$Advanced$chkMainSyndication": "on",
"Editor$Edit$Advanced$ckbPublished": "on",
"Editor$Edit$APOptions$Advancedpanel1$cklCategories$2": "1426593",
"Editor$Edit$EditorBody": "<p>222222222222jjjjjjjjjjjjjjjjjjjjjjjjjj"
"jjjjjjjjjjjjjjjjjjjjjjjjjjjjjj"
"jjjjjjrrrrrrrrrrrrrrrrrrrrrrrrr"
"rrrrrrrrrrrrrrrrrr</p>",
"Editor$Edit$lkbDraft": "存为草稿",
"Editor$Edit$txbTitle": "testinggggggggg2222222222",
}
r2 = s.post(url2, data=body, verify=False)
print("第三次打印")
print(r.content.decode("utf-8"))

  

运行后返回的结果:

F:\test-req-py\venv\Scripts\python.exe F:/test-req-py/day2/t4.py
第一次打印
<RequestsCookieJar[<Cookie ASP.NET_SessionId=z4hfksxhg2j2l0s5yuvbrljc for passport.cnblogs.com/>, <Cookie AspxAutoDetectCookieSupport=1 for passport.cnblogs.com/>, <Cookie SERVERID=33fdc71a64f9cf564b6409a0da1aa10e|1553434512|1553434512 for passport.cnblogs.com/>]>
第二次打印
<RequestsCookieJar[<Cookie .CNBlogsCookie=25D6E068A4434E2532E9F553728ACDE1F94F18E8079E07660FA168C361D87E1A59533A6BA991A7B14091F6609057C32CDE3E4BEDFDDEEA6684C7E5F96A98B77C106640480FA5B3A78EC4EEC76E9E2D5F60082CE6 for />, <Cookie .Cnblogs.AspNetCore.Cookies=CfDJ8JcopKY7yQlPr3eegllP76PlTM6nN56TphTAQZU5OFZAKIG6zQQZsZpvJ4y7IBNq4M87KdPYiThIpRzGCp0qaGQ6s30ZdqMvlqDRI0dM2k_jUprCUQ-6FZsKkco_lYEz1v9QQI_NIl85NQgwqXbjgOmErQIMWUOqhp9_zd1lVQEctfJ8Dt7aaUtO-YSPZ8glHRGQuvXghskz3MOk1IdDdnbUsytc15vJXTKtY-b7HmN2KbsMkwnnF2MSWHztAOYNU8AIuZXS3mX-UrG9PEi02UTfjkaJltqX4RwddTjTk2DI for />, <Cookie AdminCookieAlwaysExpandAdvanced=True for />, <Cookie AlwaysCreateItemsAsActive=True for />, <Cookie ASP.NET_SessionId=z4hfksxhg2j2l0s5yuvbrljc for passport.cnblogs.com/>, <Cookie AspxAutoDetectCookieSupport=1 for passport.cnblogs.com/>, <Cookie SERVERID=33fdc71a64f9cf564b6409a0da1aa10e|1553434512|1553434512 for passport.cnblogs.com/>]>
第三次打印 <!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width" />
<meta name="renderer" content="webkit" />
<meta name="force-rendering" content="webkit" />
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
<title>用户登录 - 博客园</title>
...内容太多,省略...
</head>
</body>
</html>

 

python接口自动化-session_自动发文的更多相关文章

  1. python接口自动化-Cookie_绕过验证码登录

    前言 有些登录的接口会有验证码,例如:短信验证码,图形验证码等,这种登录的验证码参数可以从后台获取(或者最直接的可查数据库) 获取不到也没关系,可以通过添加Cookie的方式绕过验证码 前面在“pyt ...

  2. python接口自动化(十)--post请求四种传送正文方式(详解)

    简介 post请求我在python接口自动化(八)--发送post请求的接口(详解)已经讲过一部分了,主要是发送一些较长的数据,还有就是数据比较安全等.我们要知道post请求四种传送正文方式首先需要先 ...

  3. python接口自动化28-requests-html爬虫框架

    前言 requests库的好,只有用过的人才知道,最近这个库的作者又出了一个好用的爬虫框架requests-html.之前解析html页面用过了lxml和bs4, requests-html集成了一些 ...

  4. python接口自动化 -参数关联(一)

    原文地址https://www.cnblogs.com/yoyoketang/p/6886610.html 原文地址https://www.cnblogs.com/yoyoketang/ 原文地址ht ...

  5. python接口自动化24-有token的接口项目使用unittest框架设计

    获取token 在做接口自动化的时候,经常会遇到多个用例需要用同一个参数token,并且这些测试用例跨.py脚本了. 一般token只需要获取一次就行了,然后其它使用unittest框架的测试用例全部 ...

  6. python接口自动化4-绕过验证码登录(cookie)

    前言 有些登录的接口会有验证码:短信验证码,图形验证码等,这种登录的话验证码参数可以从后台获取的(或者查数据库最直接). 获取不到也没关系,可以通过添加cookie的方式绕过验证码. 一.抓登录coo ...

  7. python接口自动化1-发送get请求

    前言 requests模块,也就是老污龟,为啥叫它老污龟呢,因为这个官网上的logo就是这只污龟,接下来就是学习它了. 一.环境安装 1.用pip安装requests模块 >>pip in ...

  8. python接口自动化7-参数关联

    前言 我们用自动化发帖之后,要想接着对这篇帖子操作,那就需要用参数关联了,发帖之后会有一个帖子的id,获取到这个id,继续操作传这个帖子id就可以了 (博客园的登录机制已经变了,不能用账号和密码登录了 ...

  9. 2020年第二期《python接口自动化+测试开发》课程,已开学!

    2020年第二期<python接口自动化+python测试开发>课程,12月15号开学! 主讲老师:上海-悠悠 上课方式:QQ群视频在线教学,方便交流 本期上课时间:12月15号-3月29 ...

随机推荐

  1. SQL Server函数与存储过程 计算时间

    一.通过一个开始时间.结束时间计算出一个工作日天数(不包含工作日与节假日): 1.函数 --创建函数,参数 @bengrq 开始时间,@endrq 结束时间 create function [dbo] ...

  2. ZJOI2019二轮游记

    Postscript 这个彩笔的省选随心游被中考实验考试坑掉了 所以前两天都一直脱离部队,第一天讲课完了才有的过去 一轮凉了那么二轮翻盘?翻车预定.之后还有上海的ACM没有CXR神仙的ACM窝怎么打啊 ...

  3. iOS 11: CORE ML—浅析

    本文来自于腾讯Bugly公众号(weixinBugly),未经作者同意,请勿转载,原文地址:https://mp.weixin.qq.com/s/OWD5UEiVu5JpYArcd2H9ig 作者:l ...

  4. #Java学习之路——基础阶段二(第一篇)

    我的学习阶段是跟着CZBK黑马的双源课程,学习目标以及博客是为了审查自己的学习情况,毕竟看一遍,敲一遍,和自己归纳总结一遍有着很大的区别,在此期间我会参杂Java疯狂讲义(第四版)里面的内容. 前言: ...

  5. 经典排序算法 — C#版本(中)

    归并排序比较适合大规模得数据排序,借鉴了分治思想. 归并排序原理 自古以来,分久必合合久必分. 我们可以这样理解归并排序,分-分到不能分为止,然后合并. 使用递归将问题一点一点分解,最后进行合并. 分 ...

  6. 使用 Moq 测试.NET Core 应用 -- Mock 方法

    第一篇文章, 关于Mock的概念介绍: https://www.cnblogs.com/cgzl/p/9294431.html 本文介绍使用Moq来Mock方法. 使用的代码: https://git ...

  7. qml demo分析(clocks-时钟)

    一.效果展示 效果如图1所示,时钟列表支持鼠标左右拖动,带有黑色背景的是晚上时钟,无黑色背景的是白天时钟 二.源码分析 1.main.cpp文件中只包含了一个宏,该宏的具体解释请看qml 示例中的关键 ...

  8. springboot~Mongodb的集成与使用

    说说springboot与大叔lind.ddd的渊源 Mongodb在Lind.DDD中被二次封装过(大叔的.net和.net core),将它当成是一种仓储来使用,对于开发人员来说只公开curd几个 ...

  9. Golang: Cobra命令行参数库的使用

    将cobra下载到 $GOPATH,用命令: go get -v github.com/spf13/cobra/cobra 然后使用 go install github.com/spf13/cobra ...

  10. ASP.NET Core 2.2 十八.各种Filter的内部处理机制及执行顺序

    ASP.NET core 的Filter是系统中经常用到的,本文详细分享一下各种Filter定义.执行的内部机制以及执行顺序.(ASP.NET Core 系列目录) 一. 概述 ASP.NET Cor ...