Neither ourselves nor ourselves can be offended

I always wanted to do it, but it was too troublesome to catch the package, so I just came into contact with this tool. I think it can be used in many ways, and I can record a multi-platform script. For example, when writing an article, you want to publish it simultaneously on Jianshu, ZBlog, HU60, V2EX and other platforms, all of which happen to support MarkDown.

So I first read the MD file according to the path, with the file name title, text content and then execute each publishing method, but before that, let’s first get the browser information after login and save it

Login script

Login scripts are usually executed only once at a time, as long as cookies are not out of date

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() # Save browser data to hu60 file. 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 Neither the offender nor the ourselves may be either offended or offended.Copy the code

Release the script

We can then execute the publish script directly by typing the text path

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. Brought () page. Goto (" https://cway.top/ ") page. Click (new article "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. Brought () page. Goto (" https://hu60.cn/q.php/index.index.html ") page. Click (= "text \" \ "") to posts 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 (.) = 'new articles']") Page. Click (" / / div [normalize - space (.) = 'post'] / input [normalize - space (@) type = 'text'] ") Page. The fill (" / / div [normalize - space (.) = 'post'] / 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(.)=' publish ']") page.close() context.close() browser.close() with sync_playwright() as playwright: Path = input(' Please enter md or TXT file path or drag file in console :') file = open(path, 'r') fn = file.name.split('/') # obtain title = fn[len(fn) -1].split('.')[0] # obtain 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)Copy the code