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"
);
})();
I debugged your code and found out following things which were creating issues
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"
);
})();