Welcome to our tutorial on exporting Google People Also Ask (PAA) questions and answers to a CSV file using JavaScript code and the browser console.
In this step-by-step guide, we’ll walk you through the process of extracting PAA (People Also Ask) data from Google search results. Let’s get started!
Lets search any term, keyword or question to extract the answers
Search and Open the Browser Console To begin, launch your preferred web browser and search for any term or question on Google. For instance, let’s search for a random question. Enter the search term and press the search button.
Next, open the browser console by pressing the F12 key on your keyboard. Once the console is open, you’ll be able to paste the JavaScript code snippets provided.
Step 1
Executing Code Snippet “STEP 1” In the console, paste the first code snippet labeled as “STEP 1”.
This code will trigger an automated action that gathers the necessary data from the search results.
Code snippet 1:
//STEP 1
//Iterate through all the questions, expand the answers and store the results
let arrayAnswers = [];
let arrayQuestionsAnswers = [];
let arrayQuestions = [];
let questions = document.querySelectorAll('[data-q]');
let i = 0;
for (const question of questions) {
(function(i){
window.setTimeout(function(){
arrayQuestions.push(question.innerText);
arrayAnswers.push(question.innerText);
let answer = question.querySelector('[role="button"]'); //Click the children div to expand the response
answer.focus();
answer.click();
arrayQuestionsAnswers.push(question.innerText);
if (i >= 2) { //We start again after 2 questions
j = 0;
let questions = document.querySelectorAll('[data-q]');
for (const question of questions) {
(function(j){
window.setTimeout(function(){
arrayQuestions.push(question.innerText);
let answer = question.querySelector('[role="button"]');
answer.focus();
answer.click();
arrayQuestionsAnswers.push(question.innerText);
if (j === questions.length - 1) console.log("**************** FINISHED ****************");
}, j * 5000);
}(j));
j++;
}
}
}, i * 5000);
}(i));
i++;
}
After pasting the code, the page will start scrolling automatically, expanding the questions as it collects the data. Please be patient and allow the data collection process to finish.
Step 2
Executing Code Snippet “STEP 2” Once you see the message “FINISHED” displayed in the console, you can proceed to paste the second code snippet labeled as “STEP 2”.
This code extracts all the questions and answers from the search results and saves them in a CSV file.
Code snippet 2:
//STEP 2
//Download the results as a CSV file
let arrayResults = [];
for (const questionAnswer of arrayQuestionsAnswers) {
let firstLine = questionAnswer.split('\n')[0].trim();
let restOfTheString = questionAnswer.split('\n').slice(1).join('\n');
if (!arrayResults.join('\n').includes(firstLine)) { //Only store the first line of every question if it does not exist in the arrayResults
arrayResults.push(firstLine + ';' + restOfTheString);
}
}
console.log("Final results array:");
console.log(arrayResults);
let elem = document.createElement('a');
let arrayResultsWithQuotes = [];
for (const result of arrayResults) {
arrayResultsWithQuotes.push('"' + result.split(';')[0] + '";"' + result.split(';')[1] + '"');
}
elem.href = 'data:text/csv;charset=utf-8,' + arrayResultsWithQuotes.join('\n');
elem.target = '_blank';
elem.download = 'questions.csv';
elem.click();
Upon pasting the code, a CSV file named “questions.csv” will be downloaded automatically. This file contains the questions and answers in a format suitable for spreadsheet applications such as Excel.
Congratulations!
You have successfully exported the Google People Also Ask (PAA) questions and answers from the Google search results. If you encounter any issues or need further assistance, feel free to leave a comment, and we’ll be glad to help you.
Thank you for watching, and happy data exploration! Don’t forget to subscribe to our YouTube channel to stay updated with more exciting browser console codes.