Search code examples
pythongoogle-search-api

How to exclude certain websites from google search in python?


I am using Mario Vilas' Google search API that can be found on github here: https://github.com/MarioVilas/googlesearch

Now, During the search, I want to remove certain sites from showing up. I've read the docs and it seems like there is nothing that allows us to exclude certain domains. Is there any work around for this? If not, do you know any other google search APIs that might do the trick.

Here's my code:

keyword = input("Keyword: ")
country = input("Country:")
tld_of_country = domain_names[country]



for website in search(keyword, tld=tld_of_country, num=2, 
stop=2, country="canada", pause=2): 
 try:
      links.append(website)
 except:
      continue

Solution

  • https://support.google.com/gsa/answer/2672318?hl=en

    The search query length is limited, so if you exclude too many domains using : "-website:site", Google won't return any result. In this situation, you may exclude manually from your list, using a RegEx or something similar. You could use:

    [x for x in yourlist if "domain" not in x]
    

    Or, in your situation you may add an if statement before the appending procedure.