Neculai Fantanaru

Everything Depends On The Leader

Python: How To Perform A Multiple Search On Google Using Multiple Sets Of Keys

On February 12, 2023
, in
Python Scripts Examples by Neculai Fantanaru

You can view the full code here: https://pastebin.com/pnqTfGrV

import requests
import urllib.parse
import pandas as pd
from requests_html import HTML
from requests_html import HTMLSession
import pprint

def get_source(url):
    """Return the source code for the provided URL.

    Args:
        url (string): URL of the page to scrape.

    Returns:
        response (object): HTTP response object from requests_html.
    """

    try:
        session = HTMLSession()
        response = session.get(url)
        return response

    except requests.exceptions.RequestException as e:
        print(e)

def get_results(query):

    query = urllib.parse.quote_plus(query)
    response = get_source("https://www.google.com/search?q=" + query)

    return response

def parse_results(response):

    css_identifier_result = ".tF2Cxc"
    css_identifier_title = "h3"
    css_identifier_link = ".yuRUbf a"
    css_identifier_text = ".VwiC3b"

    results = response.html.find(css_identifier_result)

    output = []

    for result in results:

        item = {
            #'title': result.find(css_identifier_title, first=True).text,
            #'link': result.find(css_identifier_link, first=True).attrs['href'],
            # Textul fara data articolului primele 15 caractere
            'text': result.find(css_identifier_text, first=True).text
        }

        output.append(item)

    return output

def google_search(query):
    # steluta e pusa automat
    cautare = '"' + query +' *"'
    print("cautarea solicitata: ", cautare)
    response = get_results(cautare)
    results = parse_results(response)
    ## Ce nu functioneaza inca
    for item in results:
        text = proceseaza_text(item['text'])
        #print(query, "---->", text)
        pozitie_text = text.find(query)
        if pozitie_text > 0:
            item['text'] = text[pozitie_text:]
    return results

def proceseaza_text(text):
    # transformam totul in litere mici
    text = text.lower()
    dictionar = {
        "ă": "a",
        "â": "a",
        "î": "i",
        "ș": "s",
        "ş": "s",
        "ț": "t",
        "ţ": "t",
        ",": "",
        "-": "",
        ".": ""
    }
    for key, value in dictionar.items():
        text = text.replace(key, value)
    return text

for cautare in ["Napoleon s-a nascut", "cutremurele din Turcia şi Siria", "cat costa un televizor Philips", "de ce are nevoie un nou nascut"]:
    pprint.pprint(google_search(cautare))


That's all folks.


Also, see my other Python Scripts ---HERE---

Alatura-te Comunitatii Neculai Fantanaru
The 63 Greatest Qualities of a Leader
Cele 63 de calităţi ale liderului

Why read this book? Because it is critical to optimizing your performance. Because it reveals the main coordinates after that are build the character and skills of the leaders, highlighting what it is important for them to increase their influence.

Leadership - Magic of Mastery
Atingerea maestrului

The essential characteristic of this book in comparison with others on the market in the same domain is that it describes through examples the ideal competences of a leader. I never claimed that it's easy to become a good leader, but if people will...

The Master Touch
Leadership - Magia măiestriei

For some leaders, "leading" resembles more to a chess game, a game of cleverness and perspicacity; for others it means a game of chance, a game they think they can win every time risking and betting everything on a single card.

Leadership Puzzle
Leadership Puzzle

I wrote this book that conjoins in a simple way personal development with leadership, just like a puzzle, where you have to match all the given pieces in order to recompose the general image.

Performance in Leading
Leadership - Pe înţelesul tuturor

The aim of this book is to offer you information through concrete examples and to show you how to obtain the capacity to make others see things from the same angle as you.

Leadership for Dummies
Leadership - Pe înţelesul tuturor

Without considering it a concord, the book is representing the try of an ordinary man - the author - who through simple words, facts and usual examples instills to the ordinary man courage and optimism in his own quest to be his own master and who knows... maybe even a leader.