Spaces:
Runtime error
Runtime error
File size: 1,751 Bytes
0b98038 b349fcc 0b98038 90d8c04 b349fcc 90d8c04 b349fcc 90d8c04 b349fcc 90d8c04 b349fcc 90d8c04 b349fcc 90d8c04 0b98038 b349fcc 0b98038 |
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 |
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()
|