美文网首页
python接口自动化之参数化

python接口自动化之参数化

作者: Doggers | 来源:发表于2019-05-01 10:31 被阅读0次

一、登录函数

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

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

image.png

二、保存草稿

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

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

image.png

三、提取postid

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

image.png

四、删除草稿

1.传个url和postid就可以了

image.png

五、参考代码:

coding:utf-8

import requests

def login(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)
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

def save_box(s, url2, title, body_data):
'''# 获取报存之后url地址'''
body = {"__VIEWSTATE": "",
"__VIEWSTATEGENERATOR":"FE27D343",
"EditorEdittxbTitle":"这是标题",
"EditorEditEditorBody":"<p>这里正文:http://www.cnblogs.com/yoyoketang/</p>",
"EditorEditAdvancedckbPublished":"on", "EditorEditAdvancedchkDisplayHomePage":"on",
"EditorEditAdvancedchkComments":"on", "EditorEditAdvancedchkMainSyndication":"on",
"EditorEditAdvancedtxbEntryName":"", "EditorEditAdvancedtxbExcerpt":"",
"EditorEditAdvancedtbEnryPassword":"", "EditorEdit$lkbDraft":"存为草稿",
}
r2 = s.post(url2, data=body, verify=False)
print r2.url
return r2.url

def get_postid(u):
'''正则提取postid'''
import re
postid = re.findall(r"postid=(.+?)&", u)
print postid # 这里是list
if len(postid) < 1:
return ''
else:
return postid[0]

def delete_box(s,url3, postid):
'''删除草稿箱'''
json3 = {"postId": postid}
r3 = s.post(url3, json=json3, verify=False)
print r3.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接口自动化之参数化

      本文链接:https://www.haomeiwen.com/subject/iipinqtx.html