Spaces:
Runtime error
Runtime error
| from playwright.sync_api import sync_playwright | |
| import os, time, random | |
| import logging | |
| # 添加日志初始化 | |
| logging.basicConfig( | |
| level=logging.INFO, | |
| format='%(asctime)s [%(levelname)s] %(message)s', | |
| handlers=[logging.StreamHandler()] | |
| ) | |
| logger = logging.getLogger(__name__) | |
| def auto_read(): | |
| logger.info("=== 程序启动 ===") | |
| try: | |
| with sync_playwright() as p: | |
| logger.info("正在启动浏览器...") | |
| # 统一使用带参数的浏览器配置 | |
| browser = p.chromium.launch( | |
| executable_path="/usr/bin/chromium", # 确认该路径是否正确 | |
| args=["--disable-gpu", "--no-sandbox"] | |
| ) | |
| page = browser.new_page() | |
| # 登录流程 | |
| page.goto("https://linux.do/login") | |
| page.type("#username", os.getenv("USERNAME")) # 确保环境变量已注入 | |
| page.type("#password", os.getenv("PASSWORD")) | |
| page.click("button.login-button") | |
| time.sleep(3) | |
| # 自动阅读循环 | |
| while True: | |
| page.goto("https://linux.do/latest") | |
| links = page.query_selector_all("a.topic-title") | |
| for link in links[:5]: | |
| page.goto(link.get_attribute("href")) | |
| page.mouse.wheel(0, random.randint(200,500)) | |
| time.sleep(random.uniform(1.5,4)) | |
| time.sleep(random.randint(600, 1200)) | |
| except Exception as e: | |
| logger.error(f"致命错误: {str(e)}", exc_info=True) | |
| finally: | |
| browser.close() # 确保浏览器关闭 | |
| if __name__ == "__main__": | |
| logger.info("开始执行主函数") | |
| auto_read() | |