This JavaScript snippet is to extract the SERP listings from site:example.com search operator. F12 into Chrome DevTools console and paste it in.
It targets specific elements on the search results page to gather titles, URLs, and snippets; each SERP listing is captured, formatted and stored in a CSV file, which triggers a file download when you execute the snippet.
Here's a breakdown of what each part of the snippet does:
Image below: example output csv file.
By running this snippet in Devtool, you're able to automatically download the titles, URLs, and snippets of Google search operator results in a clean, neatly formatted CSV file, which can be useful for data analysis, SEO research, or archiving search queries. I last used it to scrape the results from a failed domain migration I'm working on. I wanted to see what URLs are still indexed in Google.
It targets specific elements on the search results page to gather titles, URLs, and snippets; each SERP listing is captured, formatted and stored in a CSV file, which triggers a file download when you execute the snippet.
Code:
(() => {
// Create a Blob
const createCsvBlob = (data) => {
const BOM = '\uFEFF';
const csvContent = BOM + "Title,URL,Snippet\n" + data.join("\n");
return new Blob([csvContent], { type: 'text/csv;charset=utf-8;' });
};
// Lets find ind the search result elements
const results = document.querySelectorAll('.tF2Cxc');
// Extract the title, URL, and snippet from each result
const extractedResults = Array.from(results).map(result => {
const title = result.querySelector('h3').innerText.replace(/"/g, '""'); // Escape double quotes for CSV
const url = result.querySelector('a').href;
const snippet = result.querySelector('.VwiC3b').innerText.replace(/"/g, '""').replace(/\n/g, ' '); // Escape double quotes and remove newlines for CSV
return `"${title}","${url}","${snippet}"`; // Format as CSV
});
// Convert the results to a Blob
const csvBlob = createCsvBlob(extractedResults);
// Create an anchor tag to trigger download
const link = document.createElement("a");
link.href = URL.createObjectURL(csvBlob);
link.setAttribute("download", "google_search_results.csv");
document.body.appendChild(link); // Required for Firefox
// Trigger the download
link.click();
})();
Here's a breakdown of what each part of the snippet does:
- Create a CSV Blob Function: It defines a function, createCsvBlob, that takes an array of data (strings) as input. This function adds a UTF-8 Byte Order Mark (BOM) at the beginning of the CSV content to ensure the CSV file is correctly recognised as UTF-8 encoded. Then, it combines this BOM with the column titles ("Title,URL,Snippet") and the input data, joining each element with a newline character to format it as CSV. This formatted string is then used to create a Blob object with the MIME type set to 'text/csv;charset=utf-8;'. Blobs are file-like objects of immutable, raw data; they can be read as text or binary data, or converted into a readable format.
- Find Search Result Elements: The snippet uses document.querySelectorAll('.tF2Cxc') to find all elements with the class tF2Cxc, which are the containers for each Google search result.
- Extract Data from Each Result: For each search result element found, the snippet extracts the title, URL, and snippet. It ensures that double quotes within these values are escaped correctly for CSV format and removes newlines from the snippet to preserve the CSV structure. Each result's data is formatted as a CSV line with values enclosed in double quotes and separated by commas.
- Convert Results to a Blob: The extracted data (now an array of CSV-formatted strings) is passed to the createCsvBlob function to create a Blob object containing the complete CSV content.
- Trigger File Download: The snippet creates an anchor (<a>) element, setting its href attribute to a URL created from the Blob object. This URL represents the Blob data as a file. The download attribute is set to suggest a filename ("google_search_results.csv") for the downloaded file. The anchor is temporarily added to the document's body (necessary for Firefox compatibility) and programmatically clicked, triggering the file download.
Image below: example output csv file.
By running this snippet in Devtool, you're able to automatically download the titles, URLs, and snippets of Google search operator results in a clean, neatly formatted CSV file, which can be useful for data analysis, SEO research, or archiving search queries. I last used it to scrape the results from a failed domain migration I'm working on. I wanted to see what URLs are still indexed in Google.
Attachments
Last edited: