Ryanus commited on
Commit
8bd9fcf
·
verified ·
1 Parent(s): f860811

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +18 -80
app.py CHANGED
@@ -1,85 +1,23 @@
1
- import gradio as gr
2
- import time
3
- import random
4
- import os
5
- import shutil
6
- from selenium import webdriver
7
- from selenium.webdriver.firefox.service import Service
8
- from selenium.webdriver.firefox.options import Options
9
- from webdriver_manager.firefox import GeckoDriverManager
10
 
11
- # --- 核心流量生成逻辑 ---
12
- def generate_traffic(url, views, stay_duration_min, stay_duration_max):
13
- """
14
- 使用 Selenium 无头浏览器模拟访问指定 URL
15
- :param url: 目标网址
16
- :param views: 访问次数
17
- :param stay_duration_min: 每次访问停留最小时间(秒)
18
- :param stay_duration_max: 每次访问停留最大时间(秒)
19
- :return: 生成日志
20
- """
21
  if not url.startswith('http'):
22
- return "错误:请输入有效的网址(以 http:// 或 https:// 开头)。"
23
-
24
- # 配置 Firefox 无头模式
25
- options = Options()
26
- options.add_argument("--headless")
27
- options.add_argument("--disable-gpu")
28
- options.add_argument("--no-sandbox")
29
- options.add_argument("--disable-dev-shm-usage")
30
-
31
- # 🔧 关键修复:为 Hugging Face Spaces 指定 Firefox 二进制路径
32
- # Space 中 Firefox 通常安装在 /usr/bin/firefox
33
- firefox_binary_path = "/usr/bin/firefox"
34
- if not os.path.exists(firefox_binary_path):
35
- # 作为备选方案,尝试通过 which 命令查找
36
- firefox_binary_path = shutil.which("firefox")
37
-
38
- if not firefox_binary_path:
39
- return "错误:在 Space 中未找到 Firefox 浏览器。请确保已在 apt.txt 中添加 'firefox'。"
40
 
41
- options.binary_location = firefox_binary_path
42
-
43
- # 使用 webdriver-manager 自动安装和管理 GeckoDriver
44
- try:
45
- service = Service(GeckoDriverManager().install())
46
- driver = webdriver.Firefox(service=service, options=options)
47
- except Exception as e:
48
- return f"初始化浏览器失败: {e}"
49
-
50
  logs = []
51
- for i in range(views):
52
- try:
53
- driver.get(url)
54
- stay_time = random.randint(stay_duration_min, stay_duration_max)
55
- logs.append(f"✅ 第 {i+1}/{views} 次访问成功,停留 {stay_time} 秒。")
56
- time.sleep(stay_time)
57
- except Exception as e:
58
- logs.append(f"❌ 第 {i+1} 次访问失败: {e}")
59
-
60
- driver.quit()
61
- return "\n".join(logs)
62
-
63
- # --- Gradio 界面 ---
64
- with gr.Blocks(title="网站流量生成器") as demo:
65
- gr.Markdown("## 🤖 网站流量生成器\n基于 Selenium 和 Hugging Face Spaces")
66
-
67
- with gr.Row():
68
- with gr.Column():
69
- url_input = gr.Textbox(label="目标网址", placeholder="https://example.com")
70
- views_input = gr.Number(label="访问次数", value=10, minimum=1, maximum=100, precision=0)
71
- stay_min_input = gr.Number(label="最小停留时间 (秒)", value=5, minimum=1, precision=0)
72
- stay_max_input = gr.Number(label="最大停留时间 (秒)", value=15, minimum=1, precision=0)
73
- run_btn = gr.Button("🚀 开始生成流量", variant="primary")
74
 
75
- with gr.Column():
76
- output_log = gr.Textbox(label="运行日志", lines=10, max_lines=20, interactive=False)
77
-
78
- run_btn.click(
79
- fn=generate_traffic,
80
- inputs=[url_input, views_input, stay_min_input, stay_max_input],
81
- outputs=output_log
82
- )
83
-
84
- # 启动应用
85
- demo.launch()
 
1
+ from playwright.sync_api import sync_playwright
 
 
 
 
 
 
 
 
2
 
3
+ def generate_traffic_playwright(url, views, stay_duration_min, stay_duration_max):
 
 
 
 
 
 
 
 
 
4
  if not url.startswith('http'):
5
+ return "错误:请输入有效的网址。"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6
 
 
 
 
 
 
 
 
 
 
7
  logs = []
8
+ with sync_playwright() as p:
9
+ # Playwright 会自动下载和管理浏览器
10
+ browser = p.firefox.launch(headless=True)
11
+ page = browser.new_page()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
12
 
13
+ for i in range(views):
14
+ try:
15
+ page.goto(url)
16
+ stay_time = random.randint(stay_duration_min, stay_duration_max)
17
+ logs.append(f"✅ 第 {i+1}/{views} 次访问成功,停留 {stay_time} 秒。")
18
+ page.wait_for_timeout(stay_time * 1000)
19
+ except Exception as e:
20
+ logs.append(f"❌ 第 {i+1} 次访问失败: {e}")
21
+
22
+ browser.close()
23
+ return "\n".join(logs)