007 python接口 参数关联

Posted 汁虫

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了007 python接口 参数关联相关的知识,希望对你有一定的参考价值。

 

 

 

\'\'\'
时间:2018/10/03
功能:参数关联
目录:
    一: 草稿 - 批量创建
    二: 草稿 - 创建删除
    三: Error
        1 
\'\'\'   

 

一: 草稿 - 批量创建

博客园安全机制: 创建草稿箱和删除草稿箱不能太频繁

 

# coding:utf-8
import requests
import re
import time
import datetime
import urllib3
urllib3.disable_warnings() # 忽略警告


def login(s):
    # 添加cookies
    c = requests.cookies.RequestsCookieJar()    # cookie的jar包
    c.set(".CNBlogsCookie" , "29B48ADA5898E1E0E8667C6071799BA17BA3463918C53E51B4248340C6D14C35FEE26B58F48E76D08E5B8D62135D2B0A2FF3AAA6B84698FBAFD6CFA7FEEF31B4CCC192DB11E1D430055BDB3E4BBFE3B7651CB2D77") # 去7
    c.set(".Cnblogs.AspNetCore.Cookies" , "CfDJ8J0rgDI0eRtJkfTEZKR_e83xU6tHXuCcOrnTqapnuA_jH39U-KHnyGYe5ZDMHIGFEtcARkK8YgvNMo5Kp0YJ-i6F9P1nFS0HMvcgHwc3NEpRoa3HtmdFUbJpb-w8i-4OoSmIJpSB8cqE6tvfPkiiTYKjRwW6QXrqmpzsgue-kl1yR9S3iyQF0gl9MAj_fG3PJiLVLeyrpsDVU_sXWlusfFuOV2TexDtJG5IvddTjo3YTR_r2b8ukD91obe5aqd_jyl49ptLDJsqfDZMC4QPVYZpYDuLWnPPGzru24Dpw9T5jKnt8u9j1K8fJ5bRc-iJTGw; __gads=ID=b62d6c61956e6a5d:T=1537551446:S=ALNI_MYVo1OG7TaaRZDeGmjAjyifjPg6VA; _ga=GA1.2.1683585037.1537551473; _gid=GA1.2.1823315871.1537551473; SERVERID=04ead23841720026ba009cb4f597ec8c|1537551610|1537551568")
    s.cookies.update(c) # 更新cookies


def SaveDraft(s):
    # 草稿名称
    # time.sleep(0.0001)                                                  # 休眠时间 - 万分之1秒
    strTime = datetime.datetime.now().strftime(\'%Y/%m/%d %H:%M:%S %f\')  # 获取时间 - 毫秒级别
    title = "草稿箱测试 " + strTime

    # 保存草稿
    url = "https://i.cnblogs.com/EditPosts.aspx?opt=1"  # 博客园后台 - 新随笔
    body = {
        "__VIEWSTATE": "",
        "__VIEWSTATEGENERATOR":"FE27D343",
        "Editor$Edit$txbTitle": title,
        "Editor$Edit$EditorBody": title,
        "Editor$Edit$Advanced$chkComments": "on",
        "Editor$Edit$Advanced$chkMainSyndication": "on",
        "Editor$Edit$Advanced$txbEntryName": "",
        "Editor$Edit$Advanced$txbExcerpt": "",
        "Editor$Edit$Advanced$txbTag": "",
        "Editor$Edit$Advanced$tbEnryPassword": "",
        "Editor$Edit$lkbDraft": "存为草稿"
    }
    r = s.post(url, data = body, verify = False)

    # 获取ID - 草稿
    result = r.text
    strId = re.findall("href\\=\\"EditPosts\\.aspx\\?postid\\=(.+?)\\"\\>", result)    #


def DelDraft(s, strID):
    url = "https://i.cnblogs.com/post/delete" # 删除草稿箱
    body = {
        "postId": strID
    }
    r = s.post(url, json = body, verify = False)
    print(r.content.decode("utf-8"))


if __name__ == "__main__":
    s = requests.session()    # python微型浏览器
    login(s)

    nLoop = 1
    while nLoop <=5:
       SaveDraft(s)
       nLoop += 1

 

二: 草稿 - 创建删除

# coding:utf-8
import requests
import re
import datetime
import urllib3
urllib3.disable_warnings() # 忽略警告


def login(s):
    # 添加cookies
    c = requests.cookies.RequestsCookieJar()    # cookie的jar包
    c.set(".CNBlogsCookie" , "29B48ADA5898E1E0E8667C6071799BA17BA3463918C53E51B4248340C6D14C35FEE26B58F48E76D08E5B8D62135D2B0A2FF3AAA6B84698FBAFD6CFA7FEEF31B4CCC192DB11E1D430055BDB3E4BBFE3B7651CB2D77") # 去7
    c.set(".Cnblogs.AspNetCore.Cookies" , "CfDJ8J0rgDI0eRtJkfTEZKR_e83xU6tHXuCcOrnTqapnuA_jH39U-KHnyGYe5ZDMHIGFEtcARkK8YgvNMo5Kp0YJ-i6F9P1nFS0HMvcgHwc3NEpRoa3HtmdFUbJpb-w8i-4OoSmIJpSB8cqE6tvfPkiiTYKjRwW6QXrqmpzsgue-kl1yR9S3iyQF0gl9MAj_fG3PJiLVLeyrpsDVU_sXWlusfFuOV2TexDtJG5IvddTjo3YTR_r2b8ukD91obe5aqd_jyl49ptLDJsqfDZMC4QPVYZpYDuLWnPPGzru24Dpw9T5jKnt8u9j1K8fJ5bRc-iJTGw; __gads=ID=b62d6c61956e6a5d:T=1537551446:S=ALNI_MYVo1OG7TaaRZDeGmjAjyifjPg6VA; _ga=GA1.2.1683585037.1537551473; _gid=GA1.2.1823315871.1537551473; SERVERID=04ead23841720026ba009cb4f597ec8c|1537551610|1537551568")
    s.cookies.update(c) # 更新cookies


def SaveDraft(s):
    # 草稿名称
    strTime = datetime.datetime.now().strftime(\'%Y/%m/%d %H:%M:%S %f\')  # 获取时间 - 毫秒级别
    title = "草稿箱测试 " + strTime

    # 保存草稿
    url = "https://i.cnblogs.com/EditPosts.aspx?opt=1"  # 博客园后台 - 新随笔
    body = {
        "__VIEWSTATE": "",
        "__VIEWSTATEGENERATOR":"FE27D343",
        "Editor$Edit$txbTitle": title,
        "Editor$Edit$EditorBody": title,
        "Editor$Edit$Advanced$chkComments": "on",
        "Editor$Edit$Advanced$chkMainSyndication": "on",
        "Editor$Edit$Advanced$txbEntryName": "",
        "Editor$Edit$Advanced$txbExcerpt": "",
        "Editor$Edit$Advanced$txbTag": "",
        "Editor$Edit$Advanced$tbEnryPassword": "",
        "Editor$Edit$lkbDraft": "存为草稿"
    }
    r = s.post(url, data = body, verify = False)

    # 获取ID - 草稿
    result = r.text
    strId = re.findall("href\\=\\"EditPosts\\.aspx\\?postid\\=(.+?)\\"\\>", result) 
    return strId[0]


def DelDraft(s, strID):
    url = "https://i.cnblogs.com/post/delete" # 删除草稿箱
    body = {
        "postId": strID
    }
    r = s.post(url, json = body, verify = False)
    print(r.content.decode("utf-8"))


if __name__ == "__main__":
    s = requests.session()    # python微型浏览器
    login(s)

    strId = SaveDraft(s)
    DelDraft(s, strId)

 

获取ID方法
    1 创建成功后,返回数据中获取
    2 读取草稿箱页面
    3 读取后台数据库(测试环境)

 

三: Error

  

time = time.strftime("%Y/%m/%d %H:%M:%S")
title = "草稿箱测试 " + time
UnboundLocalError: local variable \'time\' referenced before assignment

 

strTime = time.strftime("%Y/%m/%d %H:%M:%S")
title = "草稿箱测试 " + strTime
# 变量名称最好不要取名 - 和系统模块、类、方法相同的名字。

 

以上是关于007 python接口 参数关联的主要内容,如果未能解决你的问题,请参考以下文章

python+pytest接口自动化(12)-参数关联

python接口自动化6-参数化关联

python+requests接口自动化3. 接口<参数>的关联与cookie提取

python+requests接口自动化3. 接口<参数>的关联与cookie提取

python接口自动化-参数关联和JSESSIONID(上个接口返回数据作为下个接口请求参数)

python接口自动化-token参数关联登录