session关联接口
Posted 悠然现南山
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了session关联接口相关的知识,希望对你有一定的参考价值。
#coding:utf-8 import requests,json,re \'\'\' session关联接口,第一步访问登录接口,headers中要有cookie,不然会登录失败,
登录成功后,添加随笔保存为草稿,第三步判断保存草稿成功
\'\'\' requests.packages.urllib3.disable_warnings() url = "https://passport.cnblogs.com/user/signin" headers = { "User-Agent":"Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (Khtml, like Gecko) Chrome/59.0.3071.104 Safari/537.36", "Content-Type":"application/json; charset=UTF-8", "Accept":"application/json, text/javascript, */*; q=0.01", "Accept-Encoding":"gzip, deflate, br", "Accept-Language":"zh-CN,zh;q=0.8", "X-Requested-With":"XMLHttpRequest", "Cookie":"AspxAutoDetectCookieSupport=1; SERVERID=227b087667da6f8e99a1165002db93f6|1504147566|1504146209" } payload = {"input1":"SZqepch8gyaT4siZXmJTawyDvPs0mYiYTarlAj6ZViwAG5K6mC2gfT2/kwqzaLSF4HrnyajRPa6gXIWUtRBTW5SC+hvUmETyNrkcUwToEHqjRoAmmq8+SFaK4v51KNVnwyk9xhiXv+VEe2gkdn+Wu7dAIoBdwEXNHmo4J89T8K0=", "input2":"WypnynD3C5ibcgFuQTZImpQvlL183850joTwgYvjwKa7hFpZq/P0Pi6MamY8ezN29oBa1n+9FAxCM4w4HxlS+X0CJmeAAkqvWGIfl3kussg/BeFPjl+UOhwtc8EGJVojs0f0wdevAs023Pr+4mVWSKBgPwQWfCwVo8a6EsaJ1qI=", "remember":False} payload = json.dumps(payload)
#session会话保持,重点关注
s = requests.session() r = s.post(url,data=payload,headers=headers,verify=False) print(r.json()) #保存草稿箱 url2 = "https://i.cnblogs.com/EditPosts.aspx?opt=1" body = {"__VIEWSTATE": "", "__VIEWSTATEGENERATOR":"FE27D343", "Editor$Edit$txbTitle":"这是我的标题:上海-悠悠", "Editor$Edit$EditorBody":"<p>这里是中文内容:http://www.cnblogs.com/yoyoketang/</p>", "Editor$Edit$Advanced$ckbPublished":"on", "Editor$Edit$Advanced$chkDisplayHomePage":"on", "Editor$Edit$Advanced$chkComments":"on", "Editor$Edit$Advanced$chkMainSyndication":"on", "Editor$Edit$lkbDraft":"存为草稿" } r2 = s.post(url2,data=body,verify=False) print(r2.content.decode(encoding=\'utf-8\')) t = re.findall(r\'value="(.+?)" maxlength\',r2.content.decode(encoding=\'utf-8\')) print(t[0]) if t[0] == \'这是我的标题:上海-悠悠\': print(\'True\') else: print(\'False\')
以上是关于session关联接口的主要内容,如果未能解决你的问题,请参考以下文章