46 lines
1.4 KiB
Python
46 lines
1.4 KiB
Python
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)
|