木偶评估功能

编程入门 行业动态 更新时间:2024-10-24 22:29:50
本文介绍了木偶评估功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我是pupetteer的新手,我试图通过一些示例来了解它的实际工作原理:

I'm new to pupetteer and I'm trying to understand how it's actually working through some examples:

因此,在此示例中,我基本上要做的是提取YouTube视频的观看次数.我已经在Chrome控制台上写了一条js行,可以提取以下信息:

So basically what I'm trying to do in this example is to extract number of views of a Youtube video. I've written a js line on the Chrome console that let me extract this information:

document.querySelector('#count > yt-view-count-renderer > span.view-count.style-scope.yt-view-count-renderer').innerText

哪个做得好.但是,当我对伪造者代码进行相同操作时,他无法识别我查询的元素.

Which worked well. However when I did the same with my pupetteer code he doesn't recognize the element I queried.

const puppeteer = require('puppeteer') const getData = async () => { const browser = await puppeteer.launch() const page = await browser.newPage() await page.goto('www.youtube/watch?v=T5GSLc-i5Xo') await page.waitFor(1000) const result = await page.evaluate(() => { let views = document.querySelector('#count > yt-view-count-renderer > span.view-count.style-scope.yt-view-count-renderer').innerText return {views} }) browser.close() return result } getData().then(value => { console.log(value) })

我终于使用ytInitialData对象做到了.但是,我想了解我的第一个代码不起作用的原因.

I finally did it using ytInitialData object. However I'd like to understand the reason why my first code didn't work.

谢谢

推荐答案

似乎等待1000个还不够.

It seems that wait for 1000 is not enough.

使用 try-puppeteer.appspot/尝试解决方案,然后您会看到.

Try your solution with try-puppeteer.appspot/ and you will see.

但是,如果尝试以下解决方案,您将获得正确的结果

However if you try the following solution, you will get the correct result

const browser = await puppeteer.launch(); const page = await browser.newPage(); await page.goto('www.youtube/watch?v=T5GSLc-i5Xo'); await page.waitForSelector('span.view-count'); const views = await page.evaluate(() => document.querySelector('span.view-count').textContent); console.log('Number of views: ' + views); await browser.close();

更多推荐

木偶评估功能

本文发布于:2023-11-06 21:42:07,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1564751.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:木偶   功能

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!