zoukankan      html  css  js  c++  java
  • requests---自动写博客

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

    自动写博客

    我们先理下书写的思路:

    1.通过request访问博客园;

    2.通过session保存cookies;

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

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

    获取登录cookies

    1.通过fiddlerF12进行抓取未登录的cookies值

    2.再次抓取FiddlerF12进行抓取登录后的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.发送请求后通过查看博客园的草稿箱查看是否保存成功

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

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

  • 相关阅读:
    数学之美
    作为一个程序员,你知道每天自己在做什么吗
    搭建ssm框架log4j日志
    webpack实践——DLLPlugin 和 DLLReferencePlugin的使用
    一些实用的技巧
    详解Vue 开发模式下跨域问题
    vue resource 携带cookie请求 vue cookie 跨域
    解决vue中element组件样式修改无效
    ES6 Promise 异步操作
    js 字符串操作函数
  • 原文地址:https://www.cnblogs.com/qican/p/11165530.html
Copyright © 2011-2022 走看看