Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
|
@@ -12,21 +12,35 @@ import os
|
|
| 12 |
|
| 13 |
def get_full_dimensions(driver):
|
| 14 |
"""
|
| 15 |
-
body
|
| 16 |
-
|
| 17 |
-
ページ全体の幅・高さを返すユーティリティ関数
|
| 18 |
"""
|
| 19 |
body_w = driver.execute_script("return document.body.scrollWidth")
|
| 20 |
body_h = driver.execute_script("return document.body.scrollHeight")
|
| 21 |
html_w = driver.execute_script("return document.documentElement.scrollWidth")
|
| 22 |
html_h = driver.execute_script("return document.documentElement.scrollHeight")
|
| 23 |
-
|
| 24 |
full_width = max(body_w, html_w)
|
| 25 |
full_height = max(body_h, html_h)
|
| 26 |
return full_width, full_height
|
| 27 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
def render_fullpage_screenshot(html_code):
|
| 29 |
-
# HTML
|
| 30 |
tmp_file = tempfile.NamedTemporaryFile(suffix=".html", delete=False)
|
| 31 |
tmp_path = tmp_file.name
|
| 32 |
tmp_file.write(html_code.encode('utf-8'))
|
|
@@ -41,35 +55,46 @@ def render_fullpage_screenshot(html_code):
|
|
| 41 |
|
| 42 |
try:
|
| 43 |
driver = webdriver.Chrome(options=options)
|
| 44 |
-
|
| 45 |
-
# 1) とりあえず適当なウィンドウサイズでページを読み込む
|
| 46 |
driver.set_window_size(1200, 800)
|
| 47 |
driver.get("file://" + tmp_path)
|
| 48 |
|
| 49 |
-
#
|
| 50 |
WebDriverWait(driver, 10).until(
|
| 51 |
EC.presence_of_element_located((By.TAG_NAME, "body"))
|
| 52 |
)
|
| 53 |
-
time.sleep(1)
|
| 54 |
-
|
| 55 |
-
# 2)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 56 |
driver.execute_script("""
|
| 57 |
document.documentElement.style.overflow = 'hidden';
|
| 58 |
document.body.style.overflow = 'hidden';
|
| 59 |
""")
|
|
|
|
| 60 |
|
| 61 |
-
#
|
| 62 |
-
|
| 63 |
-
driver.set_window_size(
|
| 64 |
-
time.sleep(
|
| 65 |
-
|
| 66 |
-
# 4) もう一度ページ全体のサイズを取得し、もし変化していれば再調整
|
| 67 |
-
new_full_width, new_full_height = get_full_dimensions(driver)
|
| 68 |
-
if (new_full_width != full_width) or (new_full_height != full_height):
|
| 69 |
-
driver.set_window_size(new_full_width, new_full_height)
|
| 70 |
-
time.sleep(2) # もう一度待機
|
| 71 |
|
| 72 |
-
#
|
| 73 |
png = driver.get_screenshot_as_png()
|
| 74 |
|
| 75 |
except Exception as e:
|
|
|
|
| 12 |
|
| 13 |
def get_full_dimensions(driver):
|
| 14 |
"""
|
| 15 |
+
<body>と<html>の scrollWidth/scrollHeight を比較して
|
| 16 |
+
ページ全体の幅・高さを返す
|
|
|
|
| 17 |
"""
|
| 18 |
body_w = driver.execute_script("return document.body.scrollWidth")
|
| 19 |
body_h = driver.execute_script("return document.body.scrollHeight")
|
| 20 |
html_w = driver.execute_script("return document.documentElement.scrollWidth")
|
| 21 |
html_h = driver.execute_script("return document.documentElement.scrollHeight")
|
|
|
|
| 22 |
full_width = max(body_w, html_w)
|
| 23 |
full_height = max(body_h, html_h)
|
| 24 |
return full_width, full_height
|
| 25 |
|
| 26 |
+
def scroll_to_bottom(driver):
|
| 27 |
+
"""
|
| 28 |
+
一旦ページの最下部までスクロールし、
|
| 29 |
+
Lazy Loadなどで追加要素が生成されるのを待つ
|
| 30 |
+
"""
|
| 31 |
+
# 現在の高さ
|
| 32 |
+
last_height = driver.execute_script("return document.body.scrollHeight")
|
| 33 |
+
while True:
|
| 34 |
+
# 最下部までスクロール
|
| 35 |
+
driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
|
| 36 |
+
time.sleep(1) # スクロール後の描画待ち
|
| 37 |
+
new_height = driver.execute_script("return document.body.scrollHeight")
|
| 38 |
+
if new_height == last_height:
|
| 39 |
+
break
|
| 40 |
+
last_height = new_height
|
| 41 |
+
|
| 42 |
def render_fullpage_screenshot(html_code):
|
| 43 |
+
# HTMLを一時ファイルに保存
|
| 44 |
tmp_file = tempfile.NamedTemporaryFile(suffix=".html", delete=False)
|
| 45 |
tmp_path = tmp_file.name
|
| 46 |
tmp_file.write(html_code.encode('utf-8'))
|
|
|
|
| 55 |
|
| 56 |
try:
|
| 57 |
driver = webdriver.Chrome(options=options)
|
| 58 |
+
# 1) とりあえず適当なウィンドウサイズで開く
|
|
|
|
| 59 |
driver.set_window_size(1200, 800)
|
| 60 |
driver.get("file://" + tmp_path)
|
| 61 |
|
| 62 |
+
# ページロード完了待ち
|
| 63 |
WebDriverWait(driver, 10).until(
|
| 64 |
EC.presence_of_element_located((By.TAG_NAME, "body"))
|
| 65 |
)
|
| 66 |
+
time.sleep(1)
|
| 67 |
+
|
| 68 |
+
# 2) ページ全体サイズを取得してウィンドウを拡張
|
| 69 |
+
# → Lazy Loadがあればスクロールして要素を読み込む
|
| 70 |
+
max_iterations = 3
|
| 71 |
+
for _ in range(max_iterations):
|
| 72 |
+
# ページ全体の幅・高さを取得
|
| 73 |
+
w1, h1 = get_full_dimensions(driver)
|
| 74 |
+
# ウィンドウをそのサイズに合わせる
|
| 75 |
+
driver.set_window_size(w1, h1)
|
| 76 |
+
time.sleep(1)
|
| 77 |
+
# 一度最下部までスクロールし、Lazy Load等を促す
|
| 78 |
+
scroll_to_bottom(driver)
|
| 79 |
+
# 再度サイズを取得
|
| 80 |
+
w2, h2 = get_full_dimensions(driver)
|
| 81 |
+
# 変化がなければ打ち切り
|
| 82 |
+
if (w1 == w2) and (h1 == h2):
|
| 83 |
+
break
|
| 84 |
+
|
| 85 |
+
# 3) 最後にスクロールバーを消したい場合はここでoverflow: hidden
|
| 86 |
driver.execute_script("""
|
| 87 |
document.documentElement.style.overflow = 'hidden';
|
| 88 |
document.body.style.overflow = 'hidden';
|
| 89 |
""")
|
| 90 |
+
time.sleep(1)
|
| 91 |
|
| 92 |
+
# overflow: hidden による再レイアウトを考慮してもう一度調整
|
| 93 |
+
w3, h3 = get_full_dimensions(driver)
|
| 94 |
+
driver.set_window_size(w3, h3)
|
| 95 |
+
time.sleep(1)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 96 |
|
| 97 |
+
# 4) 最終的にスクリーンショットを撮る
|
| 98 |
png = driver.get_screenshot_as_png()
|
| 99 |
|
| 100 |
except Exception as e:
|