[Code] PAA Scraping with Selenium [Need Someone to Improve Code]

Ramazan

Regular Member
Joined
Aug 19, 2018
Messages
441
Reaction score
310
I found an script on github but its not working who can upgrade this?

Python:
from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.firefox.options import Options
import pandas as pd
from random import randint
import time
import sys
from selenium.webdriver.common.by import By
# Variables that user has to input

query = input("Add your query: ")
clicks = input("How many questions do you want to click?: ")
lang = input("Please select your languange. (For english type en. For spanish type es. For French type fr): ")
clicks = int(clicks)  # parse string into an integer



""" Search function. It opens Google, adds query in search box clicks search. Then it looks for question box and clicks N times
each of the questions and prints them out. The more question it clicks the more answers we get

parameters: - query: Query we want to look.
            - clicks: number of times we will click on the question.
            - lang: Language we want the questions. Only English and Spanish
"""


def search(query,clicks,lang):
    
    #headless option so it doesnt open chrome everytime we run it
    chrome_options = webdriver.ChromeOptions()
    chrome_options.headless = True

    driver = webdriver.Chrome(options=chrome_options,executable_path='C:/xampp/htdocs/paa/chromedriver.exe')  # Optional argument, if not specified will search path.
  
    driver.get("https://www.google.com?hl=" + lang)

    if lang == "en":
        print('The keyword you selected is:', query)
        print(' Number of clicks we will do is:',clicks)
        print('Language you selected is:',lang)

        driver.find_element(By.XPATH,"//input[@aria-label='Search']").send_keys(query+Keys.RETURN)
        print(query)
        clickingKW(clicks,driver)

    if lang =="es":
        print('The keyword you selected is:', query)
        print(' Number of clicks we will do is:',clicks)
        print('Language you selected is:',lang)
        
        driver.find_element(By.XPATH,"//input[@aria-label='Buscar']").send_keys(query+Keys.RETURN)
        print(query)
        clickingKW(clicks,driver)

"""Function that clicks N Questions.
Parameters: - clicks: number of clicks we will use on the questions
            - driver: Driver that we are using"""

def clickingKW(clicks,driver):
    paa = driver.find_elements(By.XPATH,"//span/following-sibling::div[contains(@class,'cbphWd')]")
    #Its range because clicks is int.
    for i in range(clicks):
        print('Clicking question #',i+1)
        try:
            paa[i].click()
            time.sleep( 2 )
            paa = driver.find_elements(By.XPATH,"//span/following-sibling::div[contains(@class,'cbphWd')]")
            # for j in paa:
            #     print(format(j.text))
        except:
            continue
            raise Exception('There are no questions to Click! Index is out of Range. Please add another Keyword that contains questions')
    list_paa = []
    for j in paa:
        p = format(j.text)
        p = p.splitlines()
        
        list_paa.append(p)         

    df = pd.DataFrame(list_paa,columns=['Questions'])
    
    df = df.dropna()

#Error Handling when a query doesnt contain any questions. Error onoy occurs when dataframe is empty. Depending on Language we return different """
    if df.empty and lang == 'en':
        print('DataFrame is empty! There are no questions for your Keyword. Try a different keyword')
        data = [['No results. Please Try again or try a different Keyword','No Results','No Results']]
        df = pd.DataFrame(data,columns=['Questions','Sentiment','Magnitude'])
        print(df)

    if df.empty and lang == 'es':
        print('DataFrame vacio! No hay preguntas para tu Keyword. Porfavor intenta nuevamente con una keyword diferente')
        data = [['No hay resultados. Intenta nuevamente con una keyword diferente Please Try again or try a different Keyword','Sin Resultados','Sin Resultados']]
        df = pd.DataFrame(data,columns=['Questions','Sentiment','Magnitude'])
        print(df)

    elif lang == 'en':
        print(df)
        print('Visit https://simpletools.io to find more SEO tools like this.')
        df.to_csv(query+'.csv', index = False)

    elif lang == 'es':
        print(df)
        print('Visita https://simpletools.io para encontrar mas herramientas de SEO como esta')
        df.to_csv(query+'.csv', index = False)


search(query,clicks,lang)
 
Why go through the hassle of using selenium when you can just send a simple http request and parse the response. There is already code here in the forum for scraping PAA. Can't recall who uploaded it but will copy it here.

import requests from lxml.html import fromstring import lxml.html 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': 'en-US,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' } query = 'weight loss tips' response = requests.get(f'https://www.google.com/search?q={query}&start=0', headers=header).text tree = lxml.html.fromstring(response) node = tree.xpath('//@data-q') print(node)
 
Why go through the hassle of using selenium when you can just send a simple http request and parse the response. There is already code here in the forum for scraping PAA. Can't recall who uploaded it but will copy it here.

import requests from lxml.html import fromstring import lxml.html 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': 'en-US,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' } query = 'weight loss tips' response = requests.get(f'https://www.google.com/search?q={query}&start=0', headers=header).text tree = lxml.html.fromstring(response) node = tree.xpath('//@data-q') print(node)
Already upgraded that code on my another thread : https://www.blackhatworld.com/seo/c...ython-upgraded-code-from-iam_ironman.1423430/

I want to get 20 question and answers. This method not able to scrape after 4-5 questions because of javascript rendering.
 
Back
Top