LoginSignup
23
34

More than 5 years have passed since last update.

【Python】SeleniumでHeadless Firefoxを使おう

Last updated at Posted at 2018-09-05

目次

準備

  • selenium
  • Firefox (今回はDeveloper Editionを使用します)
  • geckodriver (Firefoxドライバー)

selenium

pipでもcondaでもインストール可能です。

>> pip install selenium
>> conda install selenium

geckodriver

condaで導入すれば,pythonパッケージと同様にバージョン管理ができるほか,パスを設定する必要もないので楽です。(pipでもあるかも)他にも,brewでインストールする方法などがあります。

>> conda install -c conda-forge geckodriver
>> brew install geckodriver

コード例

Google検索画面のHTMLを取得し表示します。

from selenium import webdriver
from selenium.webdriver.firefox.options import Options

# ブラウザーを起動
options = Options()
options.binary_location = '/Applications/Firefox Developer Edition.app/Contents/MacOS/firefox'
options.add_argument('-headless')
driver = webdriver.Firefox(firefox_options=options)

# Google検索画面にアクセス
driver.get('https://www.google.co.jp/')

# htmlを取得・表示
html = driver.page_source
print(html)

# ブラウザーを終了
driver.quit()

その他のヘッドレスブラウザ

Chrome → https://qiita.com/derodero24/items/9e9567790bde9e4b9d0c
Splash → https://qiita.com/derodero24/items/a181cd34b081bee055c7

23
34
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
23
34