一、登录函数
1.s参数是session的一个实例类,先放这里,方便写后面代码
2.登录函数传三个参数,s是需要调用前面的session类,所以必传,可以传个登录的url,然后payload是账号和密码
image.png二、保存草稿
1.编辑内容的标题title和正文body_data参数化了,这样后面可以方便传不同值
2.这里返回了获取到新的url地址,因为后面的postid参数需要在这里提取
image.png三、提取postid
1.这里用正则表达式提取url里面的postid
image.png四、删除草稿
1.传个url和postid就可以了
image.png五、参考代码:
coding:utf-8
import requests
def login(s, url, payload):
'''登录'''
先打开登录首页,获取部分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"
} # get方法其它加个ser-Agent就可以了
s = requests.session()
r = s.get(url, headers=headers,verify=False)
print s.cookies
添加登录需要的两个cookie
c = requests.cookies.RequestsCookieJar()
c.set('.CNBlogsCookie', '这里是抓到的') # 填上面抓包内容
c.set('.Cnblogs.AspNetCore.Cookies','这里是抓到的') # 填上面抓包内容
c.set('AlwaysCreateItemsAsActive',"True")
c.set('AdminCookieAlwaysExpandAdvanced',"True")
s.cookies.update(c)
print s.cookies
def save_box(s, url2, title, body_data):
'''# 获取报存之后url地址'''
body = {"__VIEWSTATE": "",
"__VIEWSTATEGENERATOR":"FE27D343",
"EditortxbTitle":"这是标题",
"EditorEditorBody":"<p>这里正文:http://www.cnblogs.com/yoyoketang/</p>",
"EditorAdvancedEditchkDisplayHomePage":"on",
"EditorAdvancedEditchkMainSyndication":"on",
"EditorAdvancedEdittxbExcerpt":"",
"EditorAdvancedEdit$lkbDraft":"存为草稿",
}
r2 = s.post(url2, data=body, verify=False)
print r2.url
return r2.url
def get_postid(u):
'''正则提取postid'''
import re
postid = re.findall(r"postid=(.+?)&", u)
print postid # 这里是list
if len(postid) < 1:
return ''
else:
return postid[0]
def delete_box(s,url3, postid):
'''删除草稿箱'''
json3 = {"postId": postid}
r3 = s.post(url3, json=json3, verify=False)
print r3.json()
if name == "main":
url = "https://passport.cnblogs.com/user/signin"
payload = {
"input1": "xxx",
"input2": "xxx",
"remember": True
}
s = requests.session()
login(s, url, payload,)
url2 = "https://i.cnblogs.com/EditPosts.aspx?opt=1"
u = save_box(s, url2, "标题", "正文内容")
postid = get_postid(u)
url3 = "https://i.cnblogs.com/post/delete"
delete_box(s, url3, postid)
网友评论