- 新增 DrissionPage 基础教程(01-05) - 新增 Playwright 基础教程(01-05) - 新增网络基础教程(01-05) - 新增 test_learning_examples.py 单元测试 - 更新 .gitignore 忽略 learning/*/output/ 目录
57 lines
1.6 KiB
Python
57 lines
1.6 KiB
Python
"""
|
|
练习 02: 用 Playwright 持久化浏览器目录保留登录态。
|
|
|
|
目标:
|
|
1. 自己导入 `sync_playwright`
|
|
2. 学会 `launch_persistent_context(user_data_dir=...)`
|
|
3. 打开一个固定页面后,手动确认这个浏览器目录会被复用
|
|
4. 理解为什么不要直接复用你日常 Chrome 默认资料目录
|
|
|
|
建议:
|
|
- 第一版先把浏览器正常启动起来
|
|
- `user_data_dir` 建议放在项目目录里,便于观察
|
|
- 如果你想验证登录态,连续运行两次并观察 cookie / 登录状态变化
|
|
|
|
运行:
|
|
./.venv/bin/python learning/playwright_basics/02_persistent_context.py
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import sys
|
|
from pathlib import Path
|
|
|
|
PROJECT_ROOT = Path(__file__).resolve().parent.parent.parent
|
|
if str(PROJECT_ROOT) not in sys.path:
|
|
sys.path.insert(0, str(PROJECT_ROOT))
|
|
|
|
USER_DATA_DIR = PROJECT_ROOT / ".playwright-douyin-profile"
|
|
DEFAULT_URL = "https://www.douyin.com/"
|
|
|
|
|
|
def get_or_create_page(context):
|
|
if getattr(context, "pages", None):
|
|
return context.pages[0]
|
|
return context.new_page()
|
|
|
|
|
|
def main() -> None:
|
|
from playwright.sync_api import sync_playwright
|
|
|
|
with sync_playwright() as playwright:
|
|
context = playwright.chromium.launch_persistent_context(
|
|
user_data_dir=str(USER_DATA_DIR),
|
|
headless=False,
|
|
)
|
|
page = get_or_create_page(context)
|
|
page.goto(DEFAULT_URL)
|
|
print(f"[INFO] 用户目录: {USER_DATA_DIR}")
|
|
print(f"[INFO] 当前 URL: {page.url}")
|
|
print(f"[INFO] 页面标题: {page.title()}")
|
|
input("观察浏览器状态后按回车退出...")
|
|
context.close()
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|