我试图从page.evaluate()我使用 Puppeteer 构建的 YouTube 抓取工具的内部获取值。我无法从page.evaluate(). 我如何实现这一目标?这是代码:
let boxes2 = []
const getData = async() => {
return await page.evaluate(async () => { // scroll till there's no more room to scroll or you get at least 250 boxes
console.log(await new Promise(resolve => {
var scrolledHeight = 0
var distance = 100
var timer = setInterval(() => {
boxes = document.querySelectorAll("div.style-scope.ytd-item-section-renderer#contents > ytd-video-renderer > div.style-scope.ytd-video-renderer#dismissable")
console.log(`${boxes.length} boxes`)
var scrollHeight = document.documentElement.scrollHeight
window.scrollBy(0, distance)
scrolledHeight += distance
if(scrolledHeight …Run Code Online (Sandbox Code Playgroud) 我正在从 youtube 上抓取数据并试图获得评论数量。我正在尝试获取包含该值的元素,但如果视频的评论被禁用,则该元素根本不存在并且waitForSelector()我认为在结束程序之前等待大约 30 秒。我如何告诉 puppeteer 等待该元素,例如 5 秒,如果它不存在,则继续执行其余代码?
这是我正在使用的代码-
await page.waitForSelector("yt-formatted-string.count-text.style-scope.ytd-comments-header-renderer")
let commentCount = await (await (await page.$("yt-formatted-string.count-text.style-scope.ytd-comments-header-renderer")).getProperty("textContent")).jsonValue()
Run Code Online (Sandbox Code Playgroud) 我在 AWS EC2 AMI (Linux) 上以无头模式运行 puppeteer 来进行一些网络抓取。是否可以从我的本地 Windows 设置远程跟踪它的 GUI?
我参考了这篇文章,想知道添加--remote-debugging-port=9222到我的代码然后http://localhost:9222在本地浏览器上访问是否会让我看到 GUI。是否有可能做到这一点?我不确定我在这里是否走在正确的轨道上。任何帮助,将不胜感激。
linux node.js chrome-remote-debugging google-chrome-headless puppeteer