1、首先手动登陆,打开fiddler抓包,将登陆前后的cookie进行对比,找出其中不同的记录
2、登陆代码如下所示
import requests import logging logging.captureWarnings(True) # 先打开登录首页,获取部分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" } #开启一个session会话 s = requests.session() r = s.get(url=url,headers=headers,verify=False) print(s.cookies) #创建一个cookie对象 c = requests.cookies.RequestsCookieJar() #添加登陆中需要的两个cookie c.set('.CNBlogsCookie', 'E03F2436FA60A1D4DC98D0F772C86D8235401909810EAF6CB131808AE341D1A4044F70C2D8FC245D088D26A6D9D0CABC08118B4771FF57EBB9EAF90C6DA075C49B010FBC0BB6CE80EB0D9D2F4570C6D5283B0C17') # 填上面抓包内容 c.set('.Cnblogs.AspNetCore.Cookies','CfDJ8FHXRRtkJWRFtU30nh_M9mChys2Bw8ClXXDqlqmNnXSJxmN5OJsmWovHs0DiVq1DxTy6LsEwU9x-Zc_oy0upWNl1PRyCpoMkfIChuLuxXI7xfaFEetn5v26kzJoUGARYhf5X23edoR56fLtBV8vLipWGoNU4-ZEQBnPGwHfpyYUZQE7bl9H41FW00bZv0izForvwwUDHtptG5W0wSI7H77rxdBOvrSUNe04NnKkp46dKhSSqhfJ3D5_zct8IfJNMvRyK-4BdOkOrLUy69QhTzVpf0Vda2FCyW0Sesy_xeH1UIgeqK-jQW2tzDbUq_m0XxQ') # 填上面抓包内容 c.set('AlwaysCreateItemsAsActive','True') c.set('AdminCookieAlwaysExpandAdvanced',"True") #更新保存的cookie信息 s.cookies.update(c) print(s.cookies)
3、登陆之后进入到发帖页面,首先手动点击,进入该页面,找到该页面的网址,然后在手动输入要填写的内容点击提交之后,抓包看其中提交的数据,将其中的数据写成字典格式,最后利用post请求提交内容,代码如下所示:
#登陆成功之后保存编辑内容 r1 = s.get('https://i.cnblogs.com/EditPosts.aspx?opt=1',headers = headers,verify=False) #保存草稿内容 url1 = 'https://i.cnblogs.com/EditPosts.aspx?opt=1' body = { '__VIEWSTATE':'', '__VIEWSTATEGENERATOR':'FE27D343', 'Editor$Edit$txbTitle':'这是满满01', 'Editor$Edit$EditorBody':'<p>满满测试</p>', 'Editor$Edit$Advanced$ckbPublished':'on', 'Editor$Edit$Advanced$chkDisplayHomePage':'on', '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':'存为草稿' } r2 = s.post(url1,data=body) print(r.status_code)
4、提交成功之后,手动进入博客园,打开草稿箱,会发现我们提交的内容已经显示在草稿箱中