Files
workspace/python/music.py
2026-03-13 20:08:33 +08:00

46 lines
1.4 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
from playwright.sync_api import sync_playwright
import time
i = 0
with sync_playwright() as p:
browser = p.chromium.launch(headless=True, slow_mo=250)
while True:
i += 1
print(f"\n执行第{i}")
context = browser.new_context()
page = context.new_page()
try:
# 加入超时和 wait_until防止 page.goto 卡死
page.goto("https://music.163.com/playlist?id=2080114727", timeout=60000, wait_until="domcontentloaded")
# 尝试等待并进入 iframe
page.wait_for_selector("iframe[name='contentFrame']", timeout=15000)
frame = page.frame(name="contentFrame")
if not frame:
print("FALSE! 没有找到 iframe")
else:
print("TRUE! 成功进入 iframe")
# 等待并点击播放按钮
frame.wait_for_selector("text=播放", timeout=20000)
print("TRUE! 找到带“播放”文字的按钮")
frame.click("text=播放")
print("CLICKED!")
# 停留 5 秒播放
page.wait_for_timeout(5000)
except Exception as e:
print(f"出错:{e.__class__.__name__}: {e}")
finally:
print("关闭 context窗口")
context.close()
# 建议加一个等待,避免过度频繁访问被封
time.sleep(40)