[CODE] People Also Ask Scraping with Python [Upgraded code from iam_ironman]

not all pages will return 3 questions so

Python:
import requests
from lxml.html import fromstring
import lxml.html

file1 = open('YourPath/PeopleAlsoAsk/keywords.txt', 'r')
file2 = open('YourPath/PeopleAlsoAsk/paa.txt', 'w')
Lines = file1.readlines()
header = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.90 Safari/537.36',
        'Accept-Language': 'tr-tr,en;q=0.9',
        'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9'
        }
for line in Lines:
        query = line.strip()
        response = requests.get(f'https://www.google.com/search?q={query}&start=0', headers=header).text
        tree = lxml.html.fromstring(response)
        nodes = tree.xpath('//@data-q')
        print(query)
        file2.write(query)
        file2.write(": ")
        for node in nodes:
            file2.write(node)
            file2.write(" , ")
        file2.write("\n")
file1.close()
file2.close()
The script is evolving. Thank you.

@Madoxx check this.
 
not all pages will return 3 questions so

Python:
import requests
from lxml.html import fromstring
import lxml.html

file1 = open('YourPath/PeopleAlsoAsk/keywords.txt', 'r')
file2 = open('YourPath/PeopleAlsoAsk/paa.txt', 'w')
Lines = file1.readlines()
header = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.90 Safari/537.36',
        'Accept-Language': 'tr-tr,en;q=0.9',
        'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9'
        }
for line in Lines:
        query = line.strip()
        response = requests.get(f'https://www.google.com/search?q={query}&start=0', headers=header).text
        tree = lxml.html.fromstring(response)
        nodes = tree.xpath('//@data-q')
        print(query)
        file2.write(query)
        file2.write(": ")
        for node in nodes:
            file2.write(node)
            file2.write(" , ")
        file2.write("\n")
file1.close()
file2.close()
fUntastic!

This will collect all of the questions, in this case, whether 1 or 5, right?
 
Is there a way to somehow collect beyond what's shown?
When I google a keyword, it shows me 4 PAAs, and when I click on the last one, it shows me two more. How can we simulate click with python to scrape additional PAAs?
 
Is there a way to somehow collect beyond what's shown?
When I google a keyword, it shows me 4 PAAs, and when I click on the last one, it shows me two more. How can we simulate click with python to scrape additional PAAs?
I'm looking for too. Need 10 question. Probably selenium is the answer.
 
I'm looking for too. Need 10 question. Probably selenium is the answer.
Lemme know if you figure out something...

If you want a table view of the results, you can:
1. change the format of the export file from .txt to .csv
2. after each result (node) write comma , without spaces

After that, you can use converter to get XLSX file which you can view in your Google Sheets :)

BTW. where can I find a list of available languages for PPA, or is it only for English?
 
Last edited:
Lemme know if you figure out something...

If you want a table view of the results, you can:
1. change the format of the export file from .txt to .csv
2. after each result (node) write comma , without spaces

After that, you can use converter to get XLSX file which you can view in your Google Sheets :)

BTW. where can I find a list of available languages for PPA, or is it only for English?
I'm too lazy for xlsx file thats why Its txt lul

If i find something i will share.
 
Is there a way to somehow collect beyond what's shown?
When I google a keyword, it shows me 4 PAAs, and when I click on the last one, it shows me two more. How can we simulate click with python to scrape additional PAAs?
In this case you need Javascript rendering using Selenium to click on the displayed questions to get more of them.
 
I'm too lazy for xlsx file thats why Its txt lul

If i find something i will share.
.csv then (same as txt, but cooler formatting).
 
Just added proxy stuff there, but i realize its only question haha.
I'm looking for too. Need 10 question. Probably selenium is the answer.
selenium kinda hard but i will try soon. i think google PAA's div class always changing . We will need to update when it start error scraping.
Scrape paa & answer + auto click + proxy + multi thread
ggwp
 
Guys, is there a way to add questions to WordPress?

I scrape the ppa using Seo Minion, and extract it in the form of exel , I have a problem uploading it to wordpress
any solution?
 
Back
Top