|
|
import gradio as gr |
|
|
import time |
|
|
import random |
|
|
from playwright.sync_api import sync_playwright |
|
|
|
|
|
|
|
|
def generate_traffic_playwright(url, views, stay_duration_min, stay_duration_max): |
|
|
""" |
|
|
使用 Playwright 无头浏览器模拟访问指定 URL |
|
|
""" |
|
|
if not url.startswith('http'): |
|
|
return "错误:请输入有效的网址(以 http:// 或 https:// 开头)。" |
|
|
|
|
|
logs = [] |
|
|
try: |
|
|
|
|
|
with sync_playwright() as p: |
|
|
browser = p.firefox.launch(headless=True) |
|
|
page = browser.new_page() |
|
|
|
|
|
for i in range(views): |
|
|
try: |
|
|
page.goto(url, timeout=20000) |
|
|
stay_time = random.randint(stay_duration_min, stay_duration_max) |
|
|
logs.append(f"✅ 第 {i+1}/{views} 次访问成功,停留 {stay_time} 秒。") |
|
|
page.wait_for_timeout(stay_time * 1000) |
|
|
except Exception as e: |
|
|
logs.append(f"❌ 第 {i+1} 次访问失败: {e}") |
|
|
|
|
|
browser.close() |
|
|
except Exception as e: |
|
|
return f"初始化 Playwright 失败: {e}\n这通常是因为浏览器未安装。请在 Space 的 'Settings' -> 'Variables' 中添加 `PLAYWRIGHT_BROWSERS_PATH=0` 并重新构建。" |
|
|
|
|
|
return "\n".join(logs) |
|
|
|
|
|
|
|
|
with gr.Blocks(title="网站流量生成器") as demo: |
|
|
gr.Markdown("## 🤖 网站流量生成器\n基于 Playwright 和 Hugging Face Spaces") |
|
|
|
|
|
with gr.Row(): |
|
|
with gr.Column(): |
|
|
url_input = gr.Textbox(label="目标网址", placeholder="https://example.com") |
|
|
views_input = gr.Number(label="访问次数", value=10, minimum=1, maximum=100, precision=0) |
|
|
stay_min_input = gr.Number(label="最小停留时间 (秒)", value=5, minimum=1, precision=0) |
|
|
stay_max_input = gr.Number(label="最大停留时间 (秒)", value=15, minimum=1, precision=0) |
|
|
run_btn = gr.Button("🚀 开始生成流量", variant="primary") |
|
|
|
|
|
with gr.Column(): |
|
|
output_log = gr.Textbox(label="运行日志", lines=10, max_lines=20, interactive=False) |
|
|
|
|
|
run_btn.click( |
|
|
fn=generate_traffic_playwright, |
|
|
inputs=[url_input, views_input, stay_min_input, stay_max_input], |
|
|
outputs=output_log |
|
|
) |
|
|
|
|
|
|
|
|
demo.launch() |