Seleniumを python と nodeで動作させてみた

February 19, 2020 – 3:38 pm

さまざまなWebサイトで公開される情報を「自動的に」収集・解析するWeb-Scraping技術のひとつに、Seleniumを用いる方法がある。Seleniumのフレームワークを用いると、リモートからブラウザ操作が可能になり、Web-Scrapingを効果的に行うことが期待される。

これまで、何度か、Seleniumを我が自宅サーバ上で動作可能にするようトライしたことがあるがことごとく失敗していた。

この数か月間、JupyterLab上でのプログラム開発環境を整備してきたところであるが、このプログラム環境を活用してSeleniumの動作環境を構築することを改めて試み、今回、なんとか成功した。動作環境の構築は、Python並びにJavaScript(Node)、それぞれの言語環境で行った。

このエントリーでは、Selenium動作環境の構築にかかわる、一連の手続きをメモしておいた。

以下、動作環境の構築・実行の手続き:

  • chromedriver のダウンロードと実行パスへの配置
    chromedriverのダウンロードサイトからlinux 64bit版をダウンロード
    unzipした後、jupyter lab上の仮想環境 (trial_env)のbin配下に配置
    Python、nodeの夫々の環境(kernel)下で、この chromedriverの動作が前提。
     
  • Python上でのSelenium実行環境を構築
    • パッケージselniumのインストール:
      (trial_env) [tsflow@localhost ~]$ conda install selenium
      Collecting package metadata (current_repodata.json): done
      Solving environment: done
      
      ## Package Plan ##
      
        environment location: /home/tsflow/anaconda3/envs/trial_env
      
        added / updated specs:
          - selenium
      
      
      The following NEW packages will be INSTALLED:
      
        selenium           pkgs/main/linux-64::selenium-3.141.0-py37h7b6447c_0
      
      
      Proceed ([y]/n)? y
      
      Preparing transaction: done
      Verifying transaction: done
      Executing transaction: done
      
    • 実行環境の構築とテスト:  
      from selenium import webdriver
      from selenium.webdriver.chrome.options import Options
      options = Options()
      
      # ヘッドレス条件の設定
      options.add_argument('--disable-gpu')
      options.add_argument('--headless')
      
      # Chromeブラウザを動作させるdriver。
      driver = webdriver.Chrome(options=options)
      
      #動作対象とするurlの指定
      url = 'https://xxxx.xxxx.com/sample.html
      driver.get(url)
      #sourceを表示
      driver.page_source
      
      driver.quit()
      

         

  • node.js上でのSeleniumでの動作環境の構築と実行
    • モジュールselenium-webdriverの npmインストール:
      (trial_env) [tsflow@localhost ~]$ npm install selenium-webdriver
      npm WARN saveError ENOENT: no such file or directory, open '/home/tsflow/package.json'
      npm WARN enoent ENOENT: no such file or directory, open '/home/tsflow/package.json'
      npm WARN tsflow No description
      npm WARN tsflow No repository field.
      npm WARN tsflow No README data
      npm WARN tsflow No license field.
      
      + selenium-webdriver@4.0.0-alpha.5
      added 1 package and audited 126 packages in 1.737s
      found 0 vulnerabilities
      
    • 実行環境の構築とテスト:   
      const chrome = require('selenium-webdriver/chrome');
      const {Builder, By, Key, until } = require('selenium-webdriver');;
      
      (async () => {
          const driver = await new Builder()
              .forBrowser('chrome')
              .setChromeOptions(new chrome.Options().headless())
              .build();
          await driver.get(src);
          html0 = await driver.getPageSource();
          await $$.html(html0);
          
          driver.quit();
      })();
      

        


Post a Comment