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
可以看到运行结果了,
这样就成功了。