#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')