Nv7-GitHub / googlesearch

A Python library for scraping the Google search engine.
https://pypi.org/project/googlesearch-python/
MIT License
513 stars 118 forks source link

googlesearch giving wrong url #26

Open Tenkho123 opened 2 years ago

Tenkho123 commented 2 years ago

So, what I'm trying to do is to open the first result of every search in google via python to access its website. Everything works fine until I insert the keyword "gmail", it's always opening a Wikipedia page like this A person from StackOverFlow suggest I come here to ask for help.

import webbrowser
from googlesearch import search

def open_website(name):
    def get_website(name):
        try:
            for web in search(name,stop=1):
                webbrowser.open(web)
                return True
        except Exception:
            return "I can't open your website, check your connection"
    if get_website(name) == None:
        return("Your website doesn't exist")
    else:
        return "Opening "+name+"..."

open_website("gmail") #you can use "facebook", "instagram", "duolingo" or something like that
Tenkho123 commented 2 years ago

These lines in the googlesearch package looks for any URLs in div elements with the g class and it finds the Wikipedia page for Gmail that you might see on the right-hand side of the search results. You'll either have to add some more filtering to your search results or submit a pull request to the package to filter out those URLs on the right. – D Malan This is the answer I received from StackOverFlow by D Malan