requests---自动写博客

  前两天写过一个关于session的博客,session登录过后,可以进行一系列的操作,今天通过模拟登录博客园,自动写博客

 

自动写博客

我们先理下书写的思路:

1.通过request访问博客园;

2.通过session保存cookies;

3.携带保存的cookies进行请求写博客的接口

4.编写博客内容,进行保存,发博客

 

获取登录cookies

1.通过fiddler\F12进行抓取未登录的cookies值

2.再次抓取Fiddler\F12进行抓取登录后的cookies值

3.把cookies放到session中

# coding:utf-8
import requests
import urllib3
from bs4 import BeautifulSoup
urllib3.disable_warnings()
s = requests.session()
s.verify = False  # 全局的
url = 'https://passport.cnblogs.com/user/signin'

headers = {
    "User-Agent":"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.81 Safari/537.36"
}
r = s.get(url,headers=headers)
cooks = {
    ".Cnblogs.AspNetCore.Cookies":"XXXXXXXX",
    ".CNBlogsCookie":"XXXXXX"
}
c = requests.cookies.RequestsCookieJar()
c.set(".CNBlogsCookie", cooks[".CNBlogsCookie"])  # 登陆有效的cookies
c.set(".Cnblogs.AspNetCore.Cookies", cooks[".Cnblogs.AspNetCore.Cookies"])
s.cookies.update(c)
url1 = "https://i.cnblogs.com/EditPosts.aspx?opt=1"
r1 = s.get(url1)
if "博客后台管理" in r1.text:
    print("登录成功!")

 

抓取写博客接口

1.抓取编写博客接口,获取编写内容,查看Raw获取接口内容

2.把这些值通过字典的形式进行编写如:{“name”:“value”}

body = {
    "__VIEWSTATE":"",
    "__VIEWSTATEGENERATOR":"FE27D343",
    "Editor$Edit$txbTitle":"编辑草稿",
    "Editor$Edit$EditorBody":"这是编辑草稿的内容",
    "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":"存为草稿"
}

 

requests自动写博客

1.重新编写草稿内容,然后通过session进行重新发帖(草稿内容部分进行了改变,方便区分)

# coding:utf-8
import requests
import urllib3
from bs4 import BeautifulSoup
urllib3.disable_warnings()
s = requests.session()
s.verify = False  # 全局的
url = 'https://passport.cnblogs.com/user/signin'

headers = {
    "User-Agent":"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.81 Safari/537.36"
}
r = s.get(url,headers=headers)
cooks = {
    ".Cnblogs.AspNetCore.Cookies":"XXXXXXXXX",
    ".CNBlogsCookie":"XXXXXX"
}
c = requests.cookies.RequestsCookieJar()
c.set(".CNBlogsCookie", cooks[".CNBlogsCookie"])  # 登陆有效的cookies
c.set(".Cnblogs.AspNetCore.Cookies", cooks[".Cnblogs.AspNetCore.Cookies"])
s.cookies.update(c)
url1 = "https://i.cnblogs.com/EditPosts.aspx?opt=1"
r1 = s.get(url1)
if "博客后台管理" in r1.text:
    print("登录成功!")
body = {
    "__VIEWSTATE":"",
    "__VIEWSTATEGENERATOR":"FE27D343",
    "Editor$Edit$txbTitle":"编辑草稿自动写博客",
    "Editor$Edit$EditorBody":"这是编辑草稿的内容自动写博客",
    "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,headers=headers,data=body)

2.发送请求后通过查看博客园的草稿箱查看是否保存成功

喜欢的朋友们,可以手动试试,好记性不如赖笔头;

如果感觉写的对您有帮助,可以点个关注哦

posted @ 2019-07-10 17:55  测试-安静  阅读(986)  评论(8编辑  收藏  举报