Created
September 1, 2025 01:33
-
-
Save datsuns/beea2f308e7d8324432df13bca00a516 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # 素の Ubuntu 22.04 | |
| FROM ubuntu:22.04 | |
| ENV DEBIAN_FRONTEND=noninteractive \ | |
| TZ=Asia/Tokyo \ | |
| PIP_NO_CACHE_DIR=1 \ | |
| PYTHONDONTWRITEBYTECODE=1 \ | |
| PYTHONUNBUFFERED=1 | |
| # 基本ツール & 日本語フォント | |
| RUN apt-get update && apt-get install -y --no-install-recommends \ | |
| ca-certificates curl wget gnupg unzip \ | |
| python3 python3-pip python3-venv \ | |
| fonts-ipafont-gothic fonts-ipafont-mincho fonts-noto-color-emoji \ | |
| tzdata \ | |
| && rm -rf /var/lib/apt/lists/* | |
| # Google Chrome の公式リポジトリを追加してインストール | |
| # 参考: https://www.google.com/linuxrepositories/ | |
| RUN mkdir -p /etc/apt/keyrings \ | |
| && wget -qO- https://dl.google.com/linux/linux_signing_key.pub | gpg --dearmor > /etc/apt/keyrings/google_linux.gpg \ | |
| && echo "deb [arch=amd64 signed-by=/etc/apt/keyrings/google_linux.gpg] http://dl.google.com/linux/chrome/deb/ stable main" \ | |
| > /etc/apt/sources.list.d/google-chrome.list \ | |
| && apt-get update && apt-get install -y --no-install-recommends \ | |
| google-chrome-stable \ | |
| && rm -rf /var/lib/apt/lists/* | |
| # 非 root ユーザで実行(Chrome の --no-sandbox を避けるため) | |
| ARG USERNAME=appuser | |
| RUN useradd -m -s /bin/bash ${USERNAME} | |
| USER ${USERNAME} | |
| WORKDIR /home/${USERNAME}/app | |
| # Python 依存のインストール | |
| COPY --chown=${USERNAME}:${USERNAME} requirements.txt . | |
| RUN python3 -m pip install -r requirements.txt | |
| # スクリプトを配置 | |
| COPY --chown=${USERNAME}:${USERNAME} run.py . | |
| # 既定の実行コマンド | |
| CMD ["python3", "run.py"] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| selenium>=4.23 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from selenium import webdriver | |
| from selenium.webdriver.chrome.options import Options | |
| from selenium.webdriver.common.by import By | |
| def build_driver(): | |
| chrome_options = Options() | |
| # ヘッドレスの新実装(安定・高速) | |
| chrome_options.add_argument("--headless=new") | |
| # コンテナで安定させるための定番 | |
| chrome_options.add_argument("--disable-dev-shm-usage") # /dev/shm が小さい環境でのクラッシュ対策 | |
| chrome_options.add_argument("--no-zygote") | |
| chrome_options.add_argument("--no-first-run") | |
| chrome_options.add_argument("--disable-gpu") | |
| chrome_options.add_argument("--window-size=1280,800") | |
| # 非 root ユーザで動かすため --no-sandbox は不要(root で動かすなら付ける) | |
| # chrome_options.add_argument("--no-sandbox") | |
| # 言語 & ロケール(日本語サイト向け) | |
| chrome_options.add_argument("--lang=ja-JP") | |
| chrome_prefs = { | |
| "intl.accept_languages": "ja-JP,ja", | |
| } | |
| chrome_options.add_experimental_option("prefs", chrome_prefs) | |
| # Selenium 4.6+ は Selenium Manager が自動で適切な chromedriver を取得 | |
| driver = webdriver.Chrome(options=chrome_options) | |
| return driver | |
| if __name__ == "__main__": | |
| url = "https://www.example.com/" | |
| driver = build_driver() | |
| try: | |
| driver.get(url) | |
| print("Title:", driver.title) | |
| # 例: 要素取得 | |
| h1 = driver.find_element(By.TAG_NAME, "h1").text | |
| print("H1:", h1) | |
| finally: | |
| driver.quit() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment