requests---自动写博客

Posted qican

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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.发送请求后通过查看博客园的草稿箱查看是否保存成功技术图片

技术图片

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

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

以上是关于requests---自动写博客的主要内容,如果未能解决你的问题,请参考以下文章

postman---postman自动发博客

今天突然觉得应该要写博客园了,若干年以后,说不定也是一方大佬,哈哈哈

.NET轻松写博客园爬虫

python自动化测试三部曲之request+django实现接口测试

用python自动登录博客园

最近写了一个博客程序: QuickBlog PHP 开源的一文多发系统