Skip to content

Instantly share code, notes, and snippets.

@shimarin
Last active April 24, 2024 06:51
Show Gist options
  • Save shimarin/e1de9125b46e70d20689de6769eb2a95 to your computer and use it in GitHub Desktop.
Save shimarin/e1de9125b46e70d20689de6769eb2a95 to your computer and use it in GitHub Desktop.
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
def image_loaded(driver, element):
try:
# 画像が完全にロードされているかどうかを確認するJavaScript
return driver.execute_script("return arguments[0].complete && typeof arguments[0].naturalWidth != 'undefined' && arguments[0].naturalWidth > 0", element)
except:
return False
def save_page_as_png(url, output_path):
# ヘッドレスモードでChromeオプションを設定
chrome_options = Options()
chrome_options.add_argument("--headless")
# Chrome WebDriverの設定
#service = webdriver.ChromeService(executable_path="/usr/bin/chromedriver")
driver = webdriver.Chrome(options=chrome_options)
driver.set_window_size(1200, 1080)
# ページにアクセス
driver.get(url)
# ページ全体のスクロールサイズを取得
total_width = driver.execute_script("return document.body.scrollWidth")
total_height = driver.execute_script("return document.body.scrollHeight")
# ページ全体をキャプチャするためにウィンドウサイズを調整
driver.set_window_size(total_width, total_height)
# スクロール
driver.execute_script('window.scrollTo(0, document.body.scrollHeight);')
# 全てのimg要素を取得
all_images = WebDriverWait(driver, 30).until(
EC.presence_of_all_elements_located((By.CSS_SELECTOR, 'img')))
visible_images = [img for img in all_images if img.is_displayed()]
# JavaScriptがページ上の全ての要素のロードを終えるまで待つ
for img in visible_images:
WebDriverWait(driver, 10).until(lambda driver: image_loaded(driver, img))
# ページを画像として保存
driver.save_screenshot(output_path)
# ブラウザを閉じる
driver.quit()
# 使用例
url = 'https://www.walbrix.co.jp/'
output_path = 'output.png'
save_page_as_png(url, output_path)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment