Playwright

什么是 playwright ?

playwright 的官网兼文档
目前我的理解是,很牛逼的自动网络执行工具,可以手工实现脚本,也可以实现录制脚本,可用的语言也包括 nodejs、python 等

它自带的浏览器有:chromium、firefox、webkit
它自带的软件有:ffmpeg,是一套可以用来记录、转换数字音频、视频,并能将其转化为流的开源计算机程序

PS:webkit 是一个开源的浏览器引擎,主要用于 Safari,以往微软把 IE 捆绑在 Windows 里(同样的 Webkit 内核的 Safari 捆绑在 Apple 产品里,Chrome 捆绑在 Google 产品里)

为什么我要学 playwright ?

说来话长,我本来在学 QR code,然后有不理解的内容,上网搜资料,一路就搜到 playwright 了,总之我想用它试试看爬虫的效果,顺便就用它自动填写健康卡填报吧

步骤

使用终端:PowerShell

# 升级 pip
python -m pip install -i https://pypi.tuna.tsinghua.edu.cn/simple --upgrade pip

# 切换清华源
pip config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple

作用:python下载库会的很快(pip install xxx)

  • 安装 PlayWright
# 安装playwright库
pip install playwright

# 安装浏览器驱动文件(安装过程稍微有点慢)
python -m playwright install
  • 切换到目标目录,创建 py 文件
cd C:\Users\xxx\Desktop
New-Item test.py
  • 要在目标网站搜索,用 chromium 驱动,将结果保存为 test.pypython 文件
python -m playwright codegen --target python -o 'test.py' -b chromium https://xxx/index.html

实战(自动填写健康卡填报)

所用到的:playwright + Github Action

所参考的:Playwright(知乎)+ Github Actions实现网站自动签到(B站)+ 方糖(知乎)

python -m playwright codegen --target python -o tt.py -b chromium https://my.zcst.edu.cn/_web/sopplus/sopplus/index.html

① 首先建立一个 Github 项目

创建好后,把自己的脚本放上去(我这里包括方糖的脚本)

  • 自动填写健康卡填报脚本
from playwright.sync_api import Playwright, sync_playwright
import sys
import time

username = sys.argv[1]
password = sys.argv[2]

def run(playwright: Playwright) -> None:
    browser = playwright.chromium.launch(headless=False)
    context = browser.new_context()
    page = context.new_page()
    page.goto("https://my.zcst.edu.cn/_web/sopplus/sopplus/index.html")
    page.goto("https://authserver.zcst.edu.cn/cas/login?service=https%3A%2F%2Fmy.zcst.edu.cn%2FportalRedirect.jsp%3F_p%3DYXM9MSZwPTEmbT1OJg__")
    page.click("[placeholder=\"用户名/手机号/邮箱\"]")
    page.fill("[placeholder=\"用户名/手机号/邮箱\"]", username)
    page.click("[placeholder=\"密码\"]")
    page.fill("[placeholder=\"密码\"]", password)
    page.click("text=登 录")
    with page.expect_popup() as popup_info:
        page.click("text=健康卡填报")
    page1 = popup_info.value
    time.sleep(5)
    with page1.expect_popup() as popup_info:
        page1.click("text=我要办理")
    page2 = popup_info.value
    time.sleep(5)
    page2.click("text=为了全力做好学校新型冠状病毒感染的肺炎疫情防控工作,我承诺以下内容填写属实。 30s 已阅读并同意 >> ins")
    page2.click("text=下一步")
    page2.click("input[name=\"xjzdz\"]")
    page2.fill("input[name=\"xjzdz\"]", "广东省珠海市XX区XX")
    page2.click("textarea[name=\"jqqx\"]")
    page2.fill("textarea[name=\"jqqx\"]", "广东省珠海市XX区XX")
    page2.click("#radio_sfqwhb3 ins")
    page2.click("#radio_sfjwhy11 ins")
    page2.click("text=在校内居住 >> ins")
    page2.click("input[name=\"jtdz\"]", modifiers=["Control"])
    page2.fill("input[name=\"jtdz\"]", "竹X, XX")
    page2.click("text=绿色 >> ins")
    page2.click("input[name=\"jrtw\"]")
    page2.fill("input[name=\"jrtw\"]", "36")
    page2.click("text=已完成三针接种 >> ins")
    page2.click("text=良好 >> ins")
    page2.click("text=无 >> ins")
    page2.click("text=本人承诺登记后、到校前不再前往其他地区 >> ins")
    page2.click("button:has-text(\"提交\")")
    page2.click("a:has-text(\"确定\")")
    page2.close()
    print("Successfully!")
    # ---------------------
    context.close()
    browser.close()
with sync_playwright() as playwright:
    run(playwright)
  • 成功返回微信方糖信息脚本
# coding=utf-8
import requests
import traceback
def msg_push(title, content, key):
    try:
        url = 'https://sc.ftqq.com/%s.send' % key
        requests.post(url, data={'text': title, 'desp': content})
    except Exception as e:
        traceback.format_exc()
if __name__ == '__main__':
    key = 'XXXXXXXXXXXXXXXXXXXXXXXXXX'
    msg_push('今日打卡成功', '自动填写健康卡填报', key)

② 在 Actions secrets 设置自己的用户名和密码

③ 在 Actions 设置 GitHub Actions

这里我有参考 Playwright GitHub Action,但是它的那句 We recommend using Playwright CLI instead of this action. 我没看懂

还有其中的 cron 需要注意,即执行日期
PS:我这里设置的(10 23 * * *)大概是早上 8 点 20 左右

  • main.yml
name: playwright

on:
  push:
    branches: [ main ]
  schedule:
    - cron: '10 23 * * *'

jobs:
  build:
    runs-on: windows-latest
    steps:
      - name: Checkout
        uses: actions/checkout@v2

      - name: 'Set up playwright'
        uses: microsoft/playwright-github-action@v1

      - name: 'Set up node'
        uses: actions/setup-node@v1

      - name: 'Set up Python'
        uses: actions/setup-python@v1
        with:
           python-version: 3.9

      - name: 'Install requirements'
        run: pip install segno && pip install playwright && playwright install

      - name: 'Working'
        run: python ./healthware.py ${{ secrets.HEALTHWARE_USERNAME }} ${{ secrets.HEALTHWARE_PASSWORD }}

      - name: 'Successfully'
        run: python ./success.py

④ 等待执行结果即可

似乎是先立即执行一遍,之后再按时执行的 ~

PS:其实我还有其他网站的脚本,但是那些是要验证码的,我又不想学了,等有空再和 Selenium 库一起细学吧 ~

posted @ 2022-01-17 23:41  筱团  阅读(926)  评论(0编辑  收藏  举报