File size: 2,452 Bytes
965e185
 
 
8bd9fcf
bb3f371
965e185
8bd9fcf
965e185
 
 
df00eeb
965e185
 
df00eeb
965e185
8bd9fcf
965e185
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8bd9fcf
965e185
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
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:
        # Playwright 会自动下载和管理浏览器
        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) # 设置20秒超时
                    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)

# --- Gradio 界面 ---
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()