0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Windows 11 + WSL2環境でのPython開発環境構築とブラウザ自動操作

Posted at

Windows 11 + WSL2環境でのPython開発環境構築とブラウザ自動操作

環境構築

WSL2とUbuntuのインストール

wsl --install
wsl --install -d Ubuntu-24.04

Python 3.11の環境構築

# deadsnakesリポジトリの追加
sudo add-apt-repository ppa:deadsnakes/ppa
sudo apt update
sudo apt install python3.11 python3.11-venv python3.11-dev

仮想環境の作成

python3.11 -m venv jupyter_env
source jupyter_env/bin/activate

パッケージのインストール

基本パッケージ

python -m pip install --upgrade pip setuptools wheel
pip install jupyter notebook jupyterlab

browser-useとその依存パッケージ

pip install browser-use

インストール確認

依存パッケージの確認コード

try:
    from maincontentextractor import MainContentExtractor
    from playwright.sync_api import sync_playwright
    import httpx
    from langchain import LangChain
    
    print("Main dependencies imported successfully!")
    
    # パッケージのバージョン確認
    import pkg_resources
    version = pkg_resources.get_distribution('browser-use').version
    print(f"browser-use version: {version}")

except ImportError as e:
    print(f"Import error: {e}")
except Exception as e:
    print(f"Error: {e}")

ブラウザ操作の基本コード

from playwright.sync_api import sync_playwright

with sync_playwright() as p:
    # ブラウザの起動
    browser = p.chromium.launch()
    
    # 新しいページを開く
    page = browser.new_page()
    
    # Googleにアクセス
    page.goto('https://www.google.com')
    
    # スクリーンショットを保存
    page.screenshot(path='screenshot.png')
    
    # ブラウザを閉じる
    browser.close()

注意点

  • Python 3.12では一部の依存パッケージに互換性の問題があるため、Python 3.11の使用を推奨
  • BrowserUseクラスは現在利用できないため、代わりにPlaywrightを直接使用
  • テレメトリーを無効化する場合は環境変数 ANONYMIZED_TELEMETRY=false を設定

トラブルシューティング

  • Jupyter Notebookが応答しない場合は、カーネルの再起動を実行
  • パッケージのインストールに問題がある場合は、--no-cache-dirオプションを使用
  • WSL2環境でブラウザの表示に問題がある場合は、headless=Trueオプションを使用
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?