Playwright 实现多端发帖

Playwright 实现多端发帖

一直想做,但是抓包太麻烦了,刚好接触到这个工具,一想其实用途还挺多,可以自己录个多平台发文脚本。例如写篇文章,想同步在简书、zblog、hu60、v2ex 等平台发布,这几个平台刚好都支持 markdown。

Playwright 实现多端发帖

那么我先根据路径读取 md 文件,以文件名为标题,文本为内容再执行各个发布方法,但在此之前,我们先获取登陆后的浏览器信息并保存吧

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)

手机扫描二维码访问

本文标题:《Playwright 实现多端发帖》作者:极四维博客
原文链接:https://cway.top/post/855.html
特别注明外均为原创,转载请注明。

分享到微信

扫描二维码

可在微信查看或分享至朋友圈。

相关文章

发表评论:

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。

«    2024年10月    »
123456
78910111213
14151617181920
21222324252627
28293031

搜索

控制面板

您好,欢迎到访网站!
  查看权限

最新留言

文章归档

  • 订阅本站的 RSS 2.0 新闻聚合