我是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();更多推荐
木偶评估功能
发布评论