python接口自动化参数化_python接口自动化-参数化

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

python接口自动化 -参数关联(一)https://www.cnblogs.com/111testing/p/9612496.html

python接口自动化-token参数关联登录(二)https://www.cnblogs.com/111testing/p/9612786.html

前言

前面一篇实现了参数的关联,那种只是记流水账的完成功能,不便于维护,也没什么可读性,接下来这篇可以把每一个动作写成一个函数,这样更方便了。

参数化的思维只需记住一点:不要写死

(由于博客园登录机制变了,登录全部用cookie,不用账号和密码登录了)

一、登录函数

1.s参数是session的一个实例类,先放这里,方便写后面代码

2.登录函数传三个参数,s是需要调用前面的session类,所以必传,可以传个登录的url,然后payload是账号和密码

+b2Za4AZe0sMAPwFbvaOS3mGm5NhrhclwwAArCfDAAAAkcgwAABAJDIMAAAQiQwDAABEIsMAAACRyDAAAEAkMgwAABCJDAMAAEQiwwAAAJHIMAAAQCQyDAAAEIkMAwAARCLDAAAAkcgwAABAJDIMAAAQiQwDAABEIsMAAACRyDAAAEAkMgwAABCJDAMAAEQiwwAAAJHIMAAAQCQyDAAAEIkMAwAARCLDAAAAkcgwAABAJDIMAAAQiQwDAABEIsMAAACRPLy7qbIss08eXh4eXm58FQAA4M2SYQAAgEh+AmNzybsn2fQpAAAAAElFTkSuQmCC

二、保存草稿

1.编辑内容的标题title和正文body_data参数化了,这样后面可以方便传不同值

2.这里返回了获取到新的url地址,因为后面的postid参数需要在这里提取

Q0Vs4e8i674XvlmMYgr2AZ5113tY4bJJwAAAOigtc4eAAAAABrTxHPNySEAAAAAyB4AAAAAmkD2AAAAANAEsgcAAACAJpA9AAAAADSB7AEAAACgCWQPAAAAAE0gewAAAABoAtkDAAAAQBPIHgAAAACaQPYAAAAA0ASyBwAAAIAmkD0AAAAANIHsAQAAAKAJZA8AAAAATSB7AAAAAGgC2QMAAABAE8geAAAAAJpA9gAAAADQBLIHAAAAgCaQPQAAAAA0gewBAAAAoAlkDwAAAABNIHsAAAAAaALZAwAAAEATyB4AAAAAmkD2AAAAANAEsgcAAACAJpA9AAAAADSB7AEAAACgCWQPAAAAAE0gewAAAABoAtkDAAAAQBPIHgAAAACa8P8DMUGh3ttkkaoAAAAASUVORK5CYII=

三、提取postid

1.这里用正则表达式提取url里面的postid

AxCCYSznPwu+AAAAAElFTkSuQmCC

四、删除草稿

1.传个url和postid就可以了

nNqcFb79J2AAAAAASUVORK5CYII=

五、参考代码:

#coding:utf-8

importrequestsdeflogin(s, url, payload):'''登录'''

#先打开登录首页,获取部分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"}#get方法其它加个ser-Agent就可以了

s=requests.session()

r= s.get(url, headers=headers,verify=False)prints.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)prints.cookiesdefsave_box(s, url2, title, body_data):'''# 获取报存之后url地址'''body= {"__VIEWSTATE": "","__VIEWSTATEGENERATOR":"FE27D343","Editor$Edit$txbTitle":"这是标题","Editor$Edit$EditorBody":"

这里正文:http://www.cnblogs.com/yoyoketang/

","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)printr2.urlreturnr2.urldefget_postid(u):'''正则提取postid'''

importre

postid= re.findall(r"postid=(.+?)&", u)print postid #这里是list

if len(postid) < 1:return ''

else:returnpostid[0]defdelete_box(s,url3, postid):'''删除草稿箱'''json3= {"postId": postid}

r3= s.post(url3, json=json3, verify=False)printr3.json()if __name__ == "__main__":

url= "https://passport.cnblogs.com/user/signin"payload={"input1": "xxx","input2": "xxx","remember": True

}

s=requests.session()

login(s, url, payload,)

url2= "https://i.cnblogs.com/EditPosts.aspx?opt=1"u= save_box(s, url2, "标题", "正文内容")

postid=get_postid(u)

url3= "https://i.cnblogs.com/post/delete"delete_box(s, url3, postid)

对python接口自动化有兴趣的,可以加python接口自动化QQ群:226296743

也可以关注下我的个人公众号:

1070438-20170417224839696-1584175751.jpg

---------------------------------python接口自动化已出书-------------------------

+O4MGN77DAAAAAAElFTkSuQmCC

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值