Google Search "Engine"

So I wrote this code a while ago, but I recently decided to update and “fix” it a bit. I might have gone a bit overboard, but anyway, it’s sort of like a Google search engine. It scrapes results from Google based on a given search term, etc.

from dataclasses import dataclass
from requests_html import HTMLSession
import urllib.parse

@dataclass
class SearchResult:
    rank: int
    url: str

@dataclass
class GoogleDomains:
    domains = (
        "https://www.google.",
        "https://google.",
        "https://webcache.googleusercontent.",
        "http://webcache.googleusercontent.",
        "https://policies.google.",
        "https://support.google.",
        "https://maps.google."
    )

class GoogleSearchEngine:
    def __init__(self):
        self.session = HTMLSession()

    @classmethod
    def clear_google_links(cls, links):
        return list(filter(lambda url: not any(domain in url for domain in GoogleDomains.domains), links))

    def search(self, term):
        search_term = urllib.parse.quote_plus(term)
        search_url = f"https://www.google.com/search?q={search_term}"

        response = self.session.get(search_url)
        links = list(response.html.absolute_links)
        links = self.clear_google_links(links)

        return [SearchResult(rank=i, url=result) for i, result in enumerate(links, start=1)]

class GoogleSearchApp:
    @staticmethod
    def run():
        print("Welcome to the Simple Google Search Engine!")
        google_search = GoogleSearchEngine()

        while True:
            print("\nOptions:")
            print("1. Search")
            print("2. Credits")
            print("3. Quit")

            choice = input("Enter your choice: ")

            options = {
                "1": lambda: GoogleSearchApp.search(google_search),
                "2": lambda: GoogleSearchApp.show_credits(),
                "3": lambda: GoogleSearchApp.quit(),
            }

            if choice in options:
                options[choice]()

    @staticmethod
    def search(google_search):
        search_term = input("Enter your search term: ")
        search_results = google_search.search(search_term)

        if not search_results:
            print("No search results found.")
        else:
            print("\nSearch Results:")
            for result in search_results:
                print(f"{result.rank}. {result.url}")

    @staticmethod
    def show_credits():
        print("\nCredits:")
        print("This Simple Google Search Engine was created by @sky.")
        print("Powered by requests-html.")

    @staticmethod
    def quit():
        print("Goodbye!")
        exit()

if __name__ == "__main__":
    GoogleSearchApp.run()
4 Likes

Isn’t it against Google’s ToS to scrape a Google website?

1 Like

It’s not scraping a site, it’s just scraping search results.