await page.wait_for_selector(".content-gallery--live-listing .content-gallery__item", timeout=10000)
except Exception:
# 如果 10 秒后目标节点仍未出现,说明确实到达了没有数据的最后一页
print(f"第 {page_num} 页未检测到有效在线主播数据,翻页结束。\n")
break
- # 每二次数组截胡
&&
+ # 此时 DOM 中必定已有数据,安全执行并集提取
+ # 每二次数组截胡
elements = await page.locator(".content-gallery--live-listing .content-gallery__item").all()
for element in elements:
- # 抓取标题与名字:
&& 或
- title_loc = element.locator(".cam-tile__title, .cam-tile__personal-info")
+ # 抓取标题与名字:兼容 .cam-tile__title 或 .cam-tile__details
+ title_loc = element.locator(".cam-tile__title, .cam-tile__details")
if await title_loc.count() > 0:
title = await title_loc.first.inner_text()
else:
title = "未知用户"
- # 抓取 href 跳转链接: 0:
- href = await href_loc.first.get_attribute("href")
+ # 抓取 href 跳转链接:对应 class="cam-tile" 等于跳转链接
+ a_loc = element.locator("a.cam-tile")
+ if await a_loc.count() > 0:
+ href = await a_loc.first.get_attribute("href")
else:
href = ""