Files
chatrebot_aireply_plug/scripts/browse.py
Claw e1fbd31c64 完整 AgentSkill:词库外部化 + 全部脚本/Skill 脱敏上传
- 高危词库从 process.py 抽到 config.toml [security] section
- process.py 改为 _get_high_risk_words() 动态加载(配置优先)
- 新增 15 个 QQ 操作脚本(已脱敏:QQ号/IP/Token → 占位符)
- 新增 8 个 SKILL.md(qq-messenger/management/resolver/napcat-extras 等)
- 新增 SKILL.md 入口(完整部署方案文档)
- 保留 SDK 框架(plugin_modules.py, file_store_api.py, package.py 等)
2026-05-04 16:23:59 +08:00

76 lines
2.5 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/usr/bin/env python3
"""
Headless browser tool for qq-agent.
Usage:
python3 browse.py https://example.com # 获取页面文字内容
python3 browse.py https://example.com --screenshot # 截图保存
python3 browse.py https://example.com --wait 3 # 等待3秒再抓取用于JS渲染页面
"""
import sys
import os
import argparse
from playwright.sync_api import sync_playwright, TimeoutError as PlaywrightTimeout
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
SAVE_DIR = os.path.join(BASE_DIR, "files")
CHROMIUM_PATH = "/usr/local/bin/chromium"
def browse(url: str, wait_sec: int = 0, screenshot: bool = False):
with sync_playwright() as p:
browser = p.chromium.launch(
executable_path=CHROMIUM_PATH,
headless=True,
args=["--no-sandbox", "--disable-setuid-sandbox"]
)
page = browser.new_page(viewport={"width": 1280, "height": 720})
page.set_default_timeout(15000)
try:
page.goto(url, wait_until="domcontentloaded")
except PlaywrightTimeout:
print("⚠️ 页面加载超时,使用已获取的内容")
if wait_sec > 0:
page.wait_for_timeout(wait_sec * 1000)
title = page.title()
print(f"标题: {title}")
print(f"URL: {url}")
print()
if screenshot:
os.makedirs(SAVE_DIR, exist_ok=True)
safe_name = "".join(c if c.isalnum() or c in '-_' else '_' for c in url[:50])
path = os.path.join(SAVE_DIR, f"screenshot_{safe_name}.png")
page.screenshot(path=path, full_page=True)
print(f"📸 截图已保存: {path}")
print()
# 提取正文文字
content = page.inner_text("body")
# 清理过长的空白行
lines = [l.strip() for l in content.split("\n")]
text = "\n".join(l for l in lines if l)
if len(text) > 5000:
print(text[:5000])
print(f"\n...内容过长仅显示前5000字符{len(text)}字符)")
else:
print(text)
browser.close()
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="无头浏览器工具")
parser.add_argument("url", help="要访问的网页URL")
parser.add_argument("--screenshot", action="store_true", help="截图保存")
parser.add_argument("--wait", type=int, default=0, help="等待秒数用于JS渲染页面")
args = parser.parse_args()
browse(args.url, wait_sec=args.wait, screenshot=args.screenshot)