Pythonでselenium headless(Firefox・Chrome)

Firefox

github.com

sudo cp geckodriver /usr/local/bin
from selenium import webdriver
from selenium.webdriver.firefox.options import Options
from bs4 import BeautifulSoup

options = Options()
options.set_headless()
driver = webdriver.Firefox(options=options)

driver.get('http://www.yahoo.co.jp')

# ブラウザ操作

driver.save_screenshot("ss.png")

html = driver.page_source
driver.quit()

soup = BeautifulSoup(html, 'html.parser')

Chrome

sites.google.com

from selenium import webdriver
from bs4 import BeautifulSoup

options = webdriver.ChromeOptions()
options.set_headless()
driver = webdriver.Chrome(chrome_options=options)

driver.get('http://www.yahoo.co.jp')

# ブラウザ操作

driver.save_screenshot("ss.png")

html = driver.page_source
driver.quit()

soup = BeautifulSoup(html, 'html.parser')