用Python+Selenium自动发布简书文章(发)文章
优采云 发布时间: 2021-02-10 12:04用Python+Selenium自动发布简书文章(发)文章
写在开头
本文介绍了Python + Selenium的使用,以自动发布短书文章。上一篇文章文章中提到了一些必要的准备说明,因此在此不再赘述。
使用说明
类似地,您仍然需要分析Jane博客的界面(请记住将默认编辑器设置为Markdown)。
write-blog-jianshu
如上图所示,要在Jane中写博客,您需要选择类别(即集合),创建一个新的文章,然后填写标题和内容。
使用auto.md的内容进行分析,标题在那里,在标题中定义;正文内容也可以通过匹配-> \ n获得。其余类别已根据规则(self_category)在注释中定义。
代码说明
main.py:程序入口类,主要负责常规匹配和解析Markdown以及调用发布发布文章
import re
import jianshu
import linecache
class Main(object):
# init
def __init__(self, file):
self.title = ''
self.content = ''
self.category = ''
self.tags = ''
# OsChina的系统分类, 设个默认值
self.osChina_sys_category = '编程语言'
# CSDN的文章分类, 设个默认值
self.csdn_article_category = '原创'
# CSDN的博客分类, 设个默认值
self.csdn_blog_category = '后端'
self.read_file(file)
# 读取MD中的title, content, self_category, self_tags, osChina_sys_category, csdn_article_category, csdn_blog_category
def read_file(self, markdown_file):
self.title = linecache.getline(markdown_file, 2).split('title: ')[1].strip('\n')
with open(markdown_file, 'r', encoding='UTF-8') as f:
self.content = f.read().split('-->\n')[1]
# 重置文件指针偏移量
f.seek(0)
for line in f.readlines():
if re.search('self_category: ', line) is not None:
self.category = line.split('self_category: ')[1].strip('\n')
elif re.search('self_tags: ', line) is not None:
self.tags = line.split('self_tags: ')[1].strip('\n')
elif re.search('osChina_sys_category: ', line) is not None:
self.osChina_sys_category = line.split('osChina_sys_category: ')[1].strip('\n')
elif re.search('csdn_article_category: ', line) is not None:
self.csdn_article_category = line.split('csdn_article_category: ')[1].strip('\n')
elif re.search('csdn_blog_category: ', line) is not None:
self.csdn_blog_category = line.split('csdn_blog_category: ')[1].strip('\n')
if __name__ == '__main__':
md_file = 'auto.md'
print("Markdown File is ", md_file)
timeout = 10
main = Main(md_file)
# 简书
jian_shu = jianshu.JianShu()
jian_shu.post(main, timeout)
authorize.py:目前,仅实现通过qq授权登录的方法
from selenium.webdriver.support.wait import WebDriverWait
# QQ授权登录, 使用前提是QQ客户端在线
def qq(driver, timeout):
# 切换到最新打开的窗口
window_handles = driver.window_handles
driver.switch_to.window(window_handles[-1])
print('qq authorize title is ', driver.title)
# 切换iframe
iframe = WebDriverWait(driver, timeout).until(lambda d: d.find_element_by_id('ptlogin_iframe'))
driver.switch_to.frame(iframe)
# 点击头像进行授权登录
login = WebDriverWait(driver, timeout).until(lambda d: d.find_element_by_xpath('//*[@id="qlogin_list"]/a[1]'))
login.click()
jianshu.py:这是Jianshu的自动Blog撰写(发布)的核心课程
import time
import authorize
from selenium import webdriver
from selenium.webdriver.support.wait import WebDriverWait
# 简书
class JianShu(object):
@staticmethod
def post(main, timeout, self_timeout=3):
# 1.跳转登陆
login = 'https://www.jianshu.com/sign_in'
driver = webdriver.Chrome()
driver.get(login)
# 2.窗口最大化
driver.maximize_window()
# 3.使用QQ授权登录
driver.find_element_by_xpath('/html/body/div[1]/div[2]/div/div/ul/li[3]/a/i').click()
driver.close()
authorize.qq(driver, timeout)
# 4.点击"写文章"
write_blog = WebDriverWait(driver, timeout).until(lambda d: d.find_element_by_xpath('/html/body/nav/div/a[2]'))
write_blog.click()
driver.close()
window_handles = driver.window_handles
driver.switch_to.window(window_handles[-1])
# 5.点击指定分类
classify = WebDriverWait(driver, timeout).until(lambda d: d.find_elements_by_class_name('_3DM7w'))
for c in classify:
html = c.get_attribute('innerHTML')
if main.category in html:
c.click()
else:
# TODO 如果分类不存在,还可以直接新建分类
pass
# 6.点击'新建文章'
time.sleep(self_timeout)
new_article = WebDriverWait(driver, timeout).until(
lambda d: d.find_element_by_xpath('//*[@id="root"]/div/div[2]/div[1]/div/div/div/div[1]/i'))
new_article.click()
article = WebDriverWait(driver, timeout).until(
lambda d: d.find_element_by_xpath('//*[@id="root"]/div/div[2]/div[1]/div/div/div/ul/li[1]'))
article.click()
# 7.填写标题, 内容
time.sleep(self_timeout)
title = driver.find_element_by_class_name('_24i7u')
title.clear()
title.send_keys(main.title)
content = driver.find_element_by_id('arthur-editor')
content.clear()
content.send_keys(main.content)
# 8.保存草稿
driver.find_element_by_xpath('//*[@id="root"]/div/div[2]/div[2]/div/div/div/div/ul/li[8]/a').click()
# 8.发布文章
# driver.find_element_by_xpath('//*[@id="root"]/div/div[2]/div[2]/div/div/div/div/ul/li[1]/a').click()
实际上,剑术还支持帐户和密码登录,但是这种登录方法还具有文本验证层,感觉比较困难,而且我目前还没有研究如何解决它,因此首先使用qq授权登录
操作效果
让我们看一下正在运行的效果图。这里的测试是保存草稿。
自动发布简报
写在最后
在Jianshu中自动编写文章的想法可能是相同的。同样,这不是唯一的方法。您可以根据代码自行调整。网页的结构也可能会更改,因此不能保证程序始终可以正常运行。最后,下一篇文章将继续介绍如何在CSDN 文章中自动编写(发送)。