Seleniumで開いたページのHTMLを取得・保存する

単一ページ

from selenium import webdriver
import time

url = "https://example.com/"

driver = webdriver.Chrome()
driver.get(url)
time.sleep(10)
html = driver.page_source

with open("filename.html", 'w') as file:
    file.write(html)

複数ページ

from selenium import webdriver
import time

target_website = ["https://example.com/?page=1",
                  "https://example.com/?page=2",
                  "https://example.com/?page=3",
                  "https://example.com/?page=4"]

outputFile = ["name1", "name2", "name3", "name4"]
count = 0

for website in target_website:
    driver = webdriver.Chrome()
    driver.get(website)
    time.sleep(10)
    html = driver.page_source

    with open(outputFile[count] + ".html", 'w') as file:
        file.write(html)

    count += 1
目次