【问题标题】:How To Scrape Youtube Comments Using Cheerio and Axios in NodeJS如何在 NodeJS 中使用 Cheerio 和 Axios 抓取 Youtube 评论
【发布时间】:2022-11-03 16:48:07
【问题描述】:

这是 yt cmets 的开发控制台

我要显示 Uncle_Ratu

这是我的输出

我尝试抓取 YouTube cmets 部分,但在执行此操作时遇到了问题。

这是我尝试过的

const express=require('express');
const app=express();
const cheerio=require('cheerio');
const axios=require('axios');

const url='https://www.youtube.com/watch?v=d2Dw-PuNZSE';

axios(url).then(response=>{
    const html=response.data;
    // console.log(html);
    const $=cheerio.load(html);
    const ytdata=[];
    $('.ytd-comment-renderer',html).each(function(){
        const author=$(this).find('div#header-author h3 a').text();
        ytdata.push({
            author
        })
    })
    console.log(ytdata);
}).catch(err=>console.log("This is an error"+err))
app.listen(8080,()=>{
    console.log("Listening on port 8080");
})


我要显示 Uncle_Ratu

【问题讨论】:

  • 抓取网页而不是使用 youtube api 获取评论数据的任何理由?

标签: node.js express web-scraping axios cheerio


【解决方案1】:

您的代码的问题是您的html 缺少div#header-author。那是因为 Youtube 动态加载数据,因此您将无法轻松抓取它。我的建议是要么使用 puppeteer(但 Youtube 可能会很快拿起你的机器人并阻止你),或者使用自定义刮板。

例如,作为Web Scraping API 的工程师,您可以通过以下方式将我们的解决方案集成到您的脚本中并获得所需的结果:

const express=require('express');
const cheerio=require('cheerio');
const axios=require('axios');

const API_KEY='<YOUR_API_KEY>'
const YOUTUBE='https://www.youtube.com/watch?v=d2Dw-PuNZSE';

const url = `https://api.webscrapingapi.com/v1?api_key=${API_KEY}&url=${YOUTUBE}&render_js=1&wait_for=10000&timeout=30000&js_instructions=%5B%7B%22action%22%3A%22scrollTo%22%2C%22selector%22%3A%22div%23title%22%2C%22timeout%22%3A5000%2C%22block%22%3A%22start%22%7D%5D`

axios(url).then(response=>{
    const html=response.data
    const $=cheerio.load(html);
    const ytdata=[];
    $('.ytd-comment-renderer',html).each(function(){
        const author=$(this).find('div#header-author h3 a').text();
        ytdata.push({
            author
        })
    })
    console.log(ytdata);
}).catch(err=>console.log("This is an error"+err))

使用第三方抓取工具的优势在于它提供了 IP 轮换、各种代理类型等功能,使 Youtube 更难检测到机器人。或者,您可以在您的项目中使用puppeteer,如下所示:

const puppeteer = require("puppeteer")
const cheerio=require('cheerio');

const main = async () => {
    const browser = await puppeteer.launch({
        headless: false,
        defaultViewport: null,
        acceptInsecureCerts: true,
    })
    const page = await browser.newPage()
    await page.goto('https://www.youtube.com/watch?v=d2Dw-PuNZSE')
    await page.waitForSelector("#header-author", {
        timeout: 10000
    })
    const html = await page.content();
    await browser.close()
    const $=cheerio.load(html);
    const ytdata=[];
    $('.ytd-comment-renderer',html).each(function(){
        const author=$(this).find('div#header-author h3 a').text();
        ytdata.push({
            author
        })
    })
    console.log(ytdata);
}

main()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-08-25
    • 2014-10-03
    • 2016-11-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-25
    • 1970-01-01
    相关资源
    最近更新 更多