Search code examples
javascriptnode.jscheerio

Cheerio web scraping is not returning the scraped links


I am trying to get the links of a google search and using node js and cheerio to scrape these links. The DOM selector with jQuery works fine in the browser console but when I run my code it outputs an empty array. I am using following code

const cheerio = require("cheerio");
const axios = require("axios").default;
(async () => {
  const getData = async (url) => {
    const { data } = await axios(url);
    const $ = cheerio.load(data);

    
     const links = Array.from($('div[class="yuRUbf"] >a')).map((a) => a.href);
    console.log(links);
  };
  getData(
    "https://www.google.com/search?q=Let+You+Love+Me+by+Rita+Ora&sxsrf=ALeKk02Hp5Segi8ShvyrREw3NLZ6p7_BKw:1622526254457&ei=Lsm1YPSzG9WX1fAPvdqTgAg&sa=N&ved=2ahUKEwj0gqSo3fXwAhXVSxUIHT3tBIAQ8tMDegQIARA7&biw=1517&bih=694"
  );
})();



Solution

  • I debugged your code and found out following things which were creating issues

    1. You were not passing the user-agent header which was not fetching the desired data so we had to mock user-agent to "browser" so that selector classes were available.
    2. While taking href of the anchor tag syntax seems wrong

    Adding the revisited code which i tested is working fine.

    const cheerio = require("cheerio");
    const axios = require("axios");
    (async () => {
        const getData = async (url) => {
            const { data } = await axios.get(url, {
                headers: {
                    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.157 Safari/537.36",
                },
            });
            const $ = cheerio.load(data);
            const links = Array.from($('div[class="yuRUbf"] >a')).map((a) => {
                return $(a).attr('href')
            });
            console.log(links);
        };
        getData(
            "https://www.google.com/search?q=Let+You+Love+Me+by+Rita+Ora&sxsrf=ALeKk02Hp5Segi8ShvyrREw3NLZ6p7_BKw:1622526254457&ei=Lsm1YPSzG9WX1fAPvdqTgAg&sa=N&ved=2ahUKEwj0gqSo3fXwAhXVSxUIHT3tBIAQ8tMDegQIARA7&biw=1517&bih=694"
        );
    })();