大唐起重 發表於 2021-1-3 23:51:00

GitHub Actions教程 使用selenium自动化

<svg xmlns="http://www.w3.org/2000/svg" style="display: none">
                        <path stroke-linecap="round" d="M5,0 0,2.5 5,5z" id="raphael-marker-block" style="-webkit-tap-highlight-color: rgba(0, 0, 0, 0)"></path>
                  </svg>
                  <h1>第一步:准备</h1>
<ul><li>在http://npm.taobao.org/mirrors/chromedriver/87.0.4280.88/下载<br> chrome 驱动chromedriver(经过测试,需要87.0.4280.88版本)</li><li>建立chrome文件夹用来存放第一步下载后的chrome(linux版本)</li><li>建立文件夹Spider用来存放requirements.txt 和爬虫文件test.py<br> 建立requirements(用来安装python包)<br> 还有test.py(用来测试)</li></ul>
<p>requirements.txt</p>
<pre><code>requests==2.23.0
lxml==4.5.1
selenium==3.141.0
</code></pre>
<p>test.py</p>
<pre><code>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()
</code></pre>
<h2>第二步:开始部署</h2>
<p>建立一个工作流<br> <img src="https://img-blog.csdnimg.cn/20210103233341763.png" alt="在这里插入图片描述"></p>
<p>在左侧点击New Workflow,之后点击Skip this and set up a workflow yourself<br> <img src="https://img-blog.csdnimg.cn/20210103233503741.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2ExMjM1NTU1Ng==,size_16,color_FFFFFF,t_70" alt="在这里插入图片描述"><br> 命名文件,以.yml后缀结尾<br> 把左侧内容删掉,<br> 填入以下信息</p>
<pre><code>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
         
</code></pre>
<p>工作流建立好commit提交后,会自动运行此工作流,点击actions<br> <img src="https://img-blog.csdnimg.cn/20210103233341763.png" alt="在这里插入图片描述"><br> 左侧会有一个名为selenium的工作流(刚刚创建的),<br> 点击右侧<img src="https://img-blog.csdnimg.cn/20210103234419687.png" alt="在这里插入图片描述"><br> 点击view workflow,再点击bulid<br> <img src="https://img-blog.csdnimg.cn/20210103234505358.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2ExMjM1NTU1Ng==,size_16,color_FFFFFF,t_70" alt="在这里插入图片描述"><br> 可以看到运行结果了,<br> <img src="https://img-blog.csdnimg.cn/20210104153646565.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2ExMjM1NTU1Ng==,size_16,color_FFFFFF,t_70" alt="在这里插入图片描述"></p>
<p>这样就成功了。</p><br><br>
来源:https://www.cnblogs.com/nmydt/p/14256312.html
頁: [1]
查看完整版本: GitHub Actions教程 使用selenium自动化