cookie绕过验证码登录
#coding:utf-8 ''' cookie绕过验证码登录,第一步先访问登录页面获取登录前的cookie,第二步
用fiddler抓到的手动登录的cookie加入cookie中,登录成功,第三步登录成功
后,添加新随笔保存为草稿
''' import requests,re 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"}
#以下是需要关注的,首先用session保持会话 s = requests.session() r = s.get(url,headers=headers,verify=False) print(s.cookies) #添加登录需要的两个cookie c = requests.cookies.RequestsCookieJar() c.set('.CNBlogsCookie','68C248862A9606EBEC754881843E0B126A133EA060D2A8D6F0F120D2BD8C5CB724DE4B12C94D851DC5603655C6C4452F86E345A7537A962EB84BDC6F907A6B8866B9690883AB4BCE702DF76BB340BD372BE67D32') c.set('.Cnblogs.AspNetCore.Cookies','CfDJ8L5AbrwfmaJLh2XTSoqz06j4vYkUtvsRLF5ibGR_1Z7nRRfKuq9qMe5QGVDwXCroNvfWDjP8XW52IFzjSrfROTzyZ8KgiBupa8nabHR4n_fFMZMjCwwdocbCq8nKHR-7NvjA08dAFPE30In0-Nxs4v1KWNcj1X-9qEFmWM0zcYXaxoWeTxMFvSj0ZQusPpFMB6pNNclrgZ3rHyV_tFzdXvkHTXeTQm0CGsqr2LFBorOmh3YY_w9T45lKY-CcaVA9Yo-p3Bn--HxN8CDuxGJPfX7UWopmeo8r7yKlqs2JlSMv') s.cookies.update(c) print(s.cookies) #登录成功后保存编辑内容 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')