const unirest = require("unirest");
const cheerio = require("cheerio");
const getOrganicData = () => {
return unirest
.get("https://www.google.com/search?q=apple+linkedin&gl=us&hl=en")
.headers({
"User-Agent":
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/101.0.4951.54 Safari/537.36",
})
.then((response) => {
let $ = cheerio.load(response.body);
let titles = [];
let links = [];
let snippets = [];
let displayedLinks = [];
$(".yuRUbf > a > h3").each((i, el) => {
titles[i] = $(el).text();
});
$(".yuRUbf > a").each((i, el) => {
links[i] = $(el).attr("href");
});
$(".g .VwiC3b ").each((i, el) => {
snippets[i] = $(el).text();
});
$(".g .yuRUbf .NJjxre .tjvcx").each((i, el) => {
displayedLinks[i] = $(el).text();
});
const organicResults = [];
for (let i = 0; i < titles.length; i++) {
organicResults[i] = {
title: titles[i],
links: links[i],
snippet: snippets[i],
displayedLink: displayedLinks[i],
};
}
console.log(organicResults)
});
};
getOrganicData();
I’m trying to write a function that will successfully scrape the first few links of a google search (for example, I can pass through the query “Javascript” and I want to get the first few results when that term is searched).
I found this blog https://serpdog.io/blog/scrape-google-search-results/ and followed it.
I ran the code on the website and it returned a blank array []. (I am using node js)
I attached the code, but it’s exactly the same as on the blog.
Any ideas on how to fix this? Or do you have a better way to get the first few search results from a search term?
Thanks!
I tried several different methods of getting the top search results, none of which worked.