ColaboratoryでSelenium

stackoverflow.com

!apt install chromium-chromedriver
!cp /usr/lib/chromium-browser/chromedriver /usr/bin
!pip install selenium

from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from bs4 import BeautifulSoup

options = webdriver.ChromeOptions()
options.add_argument('--headless')
options.add_argument('--no-sandbox')
options.add_argument('--disable-dev-shm-usage')

driver = webdriver.Chrome('chromedriver', options=options)
driver.implicitly_wait(10)
driver.get('http://www.yahoo.co.jp')

html = driver.page_source.encode('utf-8')

# ブラウザ操作
driver.save_screenshot("ss.png")

driver.quit()

soup = BeautifulSoup(html, "html.parser")
print(soup.prettify())