Playwright实现多端发帖
一直想做,但是抓包太麻烦了,刚好接触到这个工具,一想其实用途还挺多,可以自己录个多平台发文脚本。例如写篇文章,想同步在简书、zblog、hu60、v2ex等平台发布,这几个平台刚好都支持markdown。
那么我先根据路径读取md文件,以文件名为标题,文本为内容再执行各个发布方法,但在此之前,我们先获取登陆后的浏览器信息并保存吧
登陆脚本
登陆脚本一般一段时间只执行一次就行,只要cookie不过期
from playwright import sync_playwright
def hu60(playwright, name, pwd):
browser = playwright.chromium.launch(headless=False)
context = browser.newContext()
page = context.newPage()
page.goto("https://hu60.cn/q.php/user.login.html?u=index.index.html")
page.fill("input[name=\"name\"]", name)
page.fill("input[name=\"pass\"]", pwd)
page.click("input[name=\"go\"]")
page.close()
# 保存浏览器数据至hu60文件 方便发布脚本读取
context.storageState(path="hu60")
context.close()
browser.close()
def zblog(playwright, name, pwd):
browser = playwright.chromium.launch(headless=False)
context = browser.newContext()
page = context.newPage()
page.goto("https://cway.top/zb_system/login.php")
page.fill("input[name=\"edtUserName\"]", name)
page.fill("input[name=\"edtPassWord\"]", pwd)
page.close()
context.storageState(path="cway")
context.close()
browser.close()
with sync_playwright() as playwright:
hu60(playwright, '帐号', '密码')
zblog(playwright, '帐号', '密码')
复制代码
发布脚本
接着我们可以直接执行发布脚本了,键入文本路径即可
from playwright import sync_playwright
def zblog(playwright, title, content, id):
import time
browser = playwright.chromium.launch(headless=False)
context = browser.newContext(storageState="cway")
page = context.newPage()
page.goto("https://cway.top/")
page.click("text=\"新建文章\"")
page.click("input[name=\"Title\"]")
page.fill("input[name=\"Title\"]", title)
page.fill("//div[normalize-space(.)='Enjoy Markdown! coding now...x 1']/div[1]/textarea", content)
time.sleep(1)
page.selectOption("select[id=\"cmbCateID\"]", id)
page.click("input[type=\"submit\"]")
page.goto("https://cway.top/")
text = page.innerText("body")
assert title in text
page.close()
context.close()
browser.close()
def hu60(playwright, title, content, id):
browser = playwright.chromium.launch(headless=False)
context = browser.newContext(storageState="hu60")
page = context.newPage()
page.goto("https://hu60.cn/q.php/index.index.html")
page.click("text=\"发帖\"")
page.click("text=/.*" + id + ".*/")
page.click("input[name=\"title\"]")
page.fill("input[name=\"title\"]", title)
page.fill("textarea[name=\"content\"]", content)
page.click("input[name=\"go\"]")
context.close()
browser.close()
def jianshu(playwright, title, content, id):
import time
browser = playwright.chromium.launch(headless=False)
context = browser.newContext(storageState="jianshu")
page = context.newPage()
page.goto("https://www.jianshu.com/writer#/")
page.click("text=\"" + id + "\"")
with page.expect_navigation():
page.click("text=\"" + id + "\"")
page.click("//span[normalize-space(.)='新建文章']")
page.click("//div[normalize-space(.)='发布文章']/input[normalize-space(@type)='text']")
page.fill("//div[normalize-space(.)='发布文章']/input[normalize-space(@type)='text']", title)
page.click("textarea[id=\"arthur-editor\"]")
page.fill("textarea[id=\"arthur-editor\"]", content)
time.sleep(2)
page.click("//a[normalize-space(.)='发布文章']")
page.close()
context.close()
browser.close()
with sync_playwright() as playwright:
path = input('请输入md或txt文件路径或在控制台拖入文件:')
file = open(path, 'r')
fn = file.name.split('/')
# 获取标题
title = fn[len(fn) - 1].split('.')[0]
# 获取内容
content = file.read()
# 分类处理 由于每个平台文章分类不一样 酌情修改
# 指定分类
cata = 'py'
zbcata, hu60cata, jscata = '7', 'Java', 'Java'
if cata == 'java':
zbcata, hu60cata, jscata = '7', 'Java', 'Java'
if cata == 'py':
zbcata, hu60cata, jscata = '18', 'Python', 'Python'
zblog(playwright, title, content, zbcata)
hu60(playwright, title, content, hu60cata)
# jianshu(playwright, title, content, jscate)
复制代码