GitHub Actions教程 使用selenium自動化


第一步:准備

  • 在http://npm.taobao.org/mirrors/chromedriver/87.0.4280.88/下載
    chrome 驅動chromedriver(經過測試,需要87.0.4280.88版本)
  • 建立chrome文件夾用來存放第一步下載后的chrome(linux版本)
  • 建立文件夾Spider用來存放requirements.txt 和爬蟲文件test.py
    建立requirements(用來安裝python包)
    還有test.py(用來測試)

requirements.txt

requests==2.23.0
lxml==4.5.1
selenium==3.141.0

test.py

from selenium import webdriver
from selenium.webdriver.chrome.options import Options
import os

chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument('--headless')
chrome_options.add_argument('--no-sandbox')
chrome_options.add_argument('--disable-gpu')
chrome_options.add_argument('--disable-dev-shm-usage')
chromedriver = "/usr/bin/chromedriver"
os.environ["webdriver.chrome.driver"] = chromedriver
driver = webdriver.Chrome(chrome_options=chrome_options,executable_path=chromedriver)
driver.get("https://www.baidu.com")
print(driver.title)
driver.quit()

第二步:開始部署

建立一個工作流
在這里插入圖片描述

在左側點擊New Workflow,之后點擊Skip this and set up a workflow yourself
在這里插入圖片描述
命名文件,以.yml后綴結尾
把左側內容刪掉,
填入以下信息

name: selenium

# Controls when the action will run. 
on:
  # Triggers the workflow on push or pull request events but only for the main branch
  push:
    branches: [ main ]
  pull_request:
    branches: [ main ]
  # Allows you to run this workflow manually from the Actions tab
  workflow_dispatch:

# A workflow run is made up of one or more jobs that can run sequentially or in parallel
jobs:
  # This workflow contains a single job called "build"
  build:
    # The type of runner that the job will run on
    runs-on: ubuntu-latest

    # Steps represent a sequence of tasks that will be executed as part of the job
    steps:
      # Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it
      - name: Checkout
        uses: actions/checkout@v2

      # Runs a single command using the runners shell
      - name: 'Set up Python'
        uses: actions/setup-python@v1
        with:
           python-version: 3.7
      - name: 'Install requirements'
        run: pip install -r ./Spider/requirements.txt
      - name: 'Working'
        run: |
          sudo cp -p ./chrome/chromedriver /usr/bin/
          chmod -R 777 /usr/bin/chromedriver
          python ./Spider/test.py
         

工作流建立好commit提交后,會自動運行此工作流,點擊actions
在這里插入圖片描述
左側會有一個名為selenium的工作流(剛剛創建的),
點擊右側在這里插入圖片描述
點擊view workflow,再點擊bulid
在這里插入圖片描述
可以看到運行結果了,
在這里插入圖片描述

這樣就成功了。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM