gaoqilan commited on
Commit
90d8c04
·
verified ·
1 Parent(s): f158f65

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +24 -23
app.py CHANGED
@@ -11,38 +11,39 @@ logging.basicConfig(
11
  logger = logging.getLogger(__name__)
12
 
13
  def auto_read():
14
- logger.info("=== 程序启动 ===") # 启动标记
15
  try:
16
  with sync_playwright() as p:
17
  logger.info("正在启动浏览器...")
 
18
  browser = p.chromium.launch(
19
- executable_path="/usr/bin/chromium",
20
  args=["--disable-gpu", "--no-sandbox"]
21
  )
22
- # ...其余代码保持不变...
23
 
24
- with sync_playwright() as p:
25
- browser = p.chromium.launch()
26
- page = browser.new_page()
27
-
28
- # 登录
29
- page.goto("https://linux.do/login")
30
- page.type("#username", os.getenv("USERNAME"))
31
- page.type("#password", os.getenv("PASSWORD"))
32
- page.click("button.login-button")
33
- time.sleep(3)
34
-
35
- # 自动阅读
36
- while True:
37
- page.goto("https://linux.do/latest")
38
- links = page.query_selector_all("a.topic-title")
39
- for link in links[:5]:
40
- page.goto(link.get_attribute("href"))
41
- page.mouse.wheel(0, random.randint(200,500))
42
- time.sleep(random.uniform(1.5,4))
43
- time.sleep(random.randint(600, 1200)) # 10-20分钟间隔
44
  except Exception as e:
45
  logger.error(f"致命错误: {str(e)}", exc_info=True)
 
 
 
46
 
47
  if __name__ == "__main__":
48
  logger.info("开始执行主函数")
 
11
  logger = logging.getLogger(__name__)
12
 
13
  def auto_read():
14
+ logger.info("=== 程序启动 ===")
15
  try:
16
  with sync_playwright() as p:
17
  logger.info("正在启动浏览器...")
18
+ # 统一使用带参数的浏览器配置
19
  browser = p.chromium.launch(
20
+ executable_path="/usr/bin/chromium", # 确认该路径是否正确
21
  args=["--disable-gpu", "--no-sandbox"]
22
  )
23
+ page = browser.new_page()
24
 
25
+ # 登录流程
26
+ page.goto("https://linux.do/login")
27
+ page.type("#username", os.getenv("USERNAME")) # 确保环境变量已注入
28
+ page.type("#password", os.getenv("PASSWORD"))
29
+ page.click("button.login-button")
30
+ time.sleep(3)
31
+
32
+ # 自动阅读循环
33
+ while True:
34
+ page.goto("https://linux.do/latest")
35
+ links = page.query_selector_all("a.topic-title")
36
+ for link in links[:5]:
37
+ page.goto(link.get_attribute("href"))
38
+ page.mouse.wheel(0, random.randint(200,500))
39
+ time.sleep(random.uniform(1.5,4))
40
+ time.sleep(random.randint(600, 1200))
41
+
 
 
 
42
  except Exception as e:
43
  logger.error(f"致命错误: {str(e)}", exc_info=True)
44
+ finally:
45
+ browser.close() # 确保浏览器关闭
46
+
47
 
48
  if __name__ == "__main__":
49
  logger.info("开始执行主函数")