python接口自动化-session_自动发文
一、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_自动发文的更多相关文章
- python接口自动化-Cookie_绕过验证码登录
前言 有些登录的接口会有验证码,例如:短信验证码,图形验证码等,这种登录的验证码参数可以从后台获取(或者最直接的可查数据库) 获取不到也没关系,可以通过添加Cookie的方式绕过验证码 前面在“pyt ...
- python接口自动化(十)--post请求四种传送正文方式(详解)
简介 post请求我在python接口自动化(八)--发送post请求的接口(详解)已经讲过一部分了,主要是发送一些较长的数据,还有就是数据比较安全等.我们要知道post请求四种传送正文方式首先需要先 ...
- python接口自动化28-requests-html爬虫框架
前言 requests库的好,只有用过的人才知道,最近这个库的作者又出了一个好用的爬虫框架requests-html.之前解析html页面用过了lxml和bs4, requests-html集成了一些 ...
- python接口自动化 -参数关联(一)
原文地址https://www.cnblogs.com/yoyoketang/p/6886610.html 原文地址https://www.cnblogs.com/yoyoketang/ 原文地址ht ...
- python接口自动化24-有token的接口项目使用unittest框架设计
获取token 在做接口自动化的时候,经常会遇到多个用例需要用同一个参数token,并且这些测试用例跨.py脚本了. 一般token只需要获取一次就行了,然后其它使用unittest框架的测试用例全部 ...
- python接口自动化4-绕过验证码登录(cookie)
前言 有些登录的接口会有验证码:短信验证码,图形验证码等,这种登录的话验证码参数可以从后台获取的(或者查数据库最直接). 获取不到也没关系,可以通过添加cookie的方式绕过验证码. 一.抓登录coo ...
- python接口自动化1-发送get请求
前言 requests模块,也就是老污龟,为啥叫它老污龟呢,因为这个官网上的logo就是这只污龟,接下来就是学习它了. 一.环境安装 1.用pip安装requests模块 >>pip in ...
- python接口自动化7-参数关联
前言 我们用自动化发帖之后,要想接着对这篇帖子操作,那就需要用参数关联了,发帖之后会有一个帖子的id,获取到这个id,继续操作传这个帖子id就可以了 (博客园的登录机制已经变了,不能用账号和密码登录了 ...
- 2020年第二期《python接口自动化+测试开发》课程,已开学!
2020年第二期<python接口自动化+python测试开发>课程,12月15号开学! 主讲老师:上海-悠悠 上课方式:QQ群视频在线教学,方便交流 本期上课时间:12月15号-3月29 ...
随机推荐
- SQLServer之删除用户自定义数据库用户
删除用户自定义数据库用户注意事项 不能从数据库中删除拥有安全对象的用户. 必须先删除或转移安全对象的所有权,才能删除拥有这些安全对象的数据库用户. 不能删除 guest 用户,但可在除 master ...
- SQL注入攻击的常见方式及测试方法
本文主要针对SQL注入的含义.以及如何进行SQL注入和如何预防SQL注入让小伙伴有个了解.适用的人群主要是测试人员,了解如何进行SQL注入,可以帮助我们测试登录.发布等模块的SQL攻击漏洞,至于如何预 ...
- Leetcode 题解
Leetcode Solutions Language: javascript c mysql Last updated: 2019-01-04 https://github.com/nusr/lee ...
- python maximum recursion depth exceeded 处理办法
1.在执行命令 pyinstaller -F D:\py\programe\banksystem.py打包生成.exe文件时报错:python maximum recursion depth exce ...
- Python基础(os模块)
os模块用于操作系统级别的操作: os.getcwd() 获取当前工作目录,即当前python脚本工作的目录路径 os.chdir("dirname") 改变当前脚本工作目录:相当 ...
- .NET Core微服务之基于Steeltoe集成Zuul实现统一API网关
Tip: 此篇已加入.NET Core微服务基础系列文章索引,本篇接上一篇<基于Steeltoe使用Eureka实现服务注册与发现>,所演示的示例也是基于上一篇的基础上而扩展的. => ...
- 【朝花夕拾】Android性能篇之(七)Android跨进程通信篇
前言 只要是面试高级工程师岗位,Android跨进程通信就是最受面试官青睐的知识点之一.Android系统的运行由大量相互独立的进程相互协助来完成的,所以Android进程间通信问题,是做好Andro ...
- 知识小罐头05(tomcat8请求源码分析 上)
这一篇我们不看源码,就大概理一下Tomcat内部组成部分!前面花费了两篇博客的篇幅来说说了一般的maven web项目并部署到tomcat运行,其实都是为这篇做铺垫的! 其实我下载了tomcat7,t ...
- VC6.0打开或添加工程时崩溃的解决方法
官方解决办法(英文):http://support.microsoft.com/kb/241396/en-us 网友解决(中文):http://blog.163.com/wjatnx@yeah/blo ...
- 微信公众号开发C#系列-5、用户和用户组管理-支持同步
1.概述 眼前时下流行的经济有个叫粉丝经济,粉丝带动收益.一个好运营良好的公众号肯定会有一大批的粉丝团,如何挖掘粉丝来产生效益,是微信营销的关键.微信公众号后台本身提供了粉丝(用户)与用户分组的管理, ...