Most of them I tried using, but didn't work out for me or gave errors like search module not found despite importing packages. Or I did work out with selenium web driver and it works great if used with Firefox or chrome or Phantom web browser, but still I felt it was a bit slow in terms of execution time, as it queried browser first and then returned search result.
So I thought of using google api and it works amazingly quick and returns results accurately.
Before I share the code here are few quick tips to follow:-
- Register on Google Api to get a Google Api key (free version)
- Now search for Google Custom Search and set up your free account to get a custom search id
- Now add this package(google-api-python-client) in your python project
(can be done by writing !pip install google-api-python-client )
That is it and all you have to do now is run this code:-
from googleapiclient.discovery import build
my_api_key = "your API KEY TYPE HERE"
my_cse_id = "YOUR CUSTOM SEARCH ENGINE ID TYPE HERE"
def google_search(search_term, api_key, cse_id, **kwargs):
service = build("customsearch", "v1", developerKey=api_key)
res = service.cse().list(q=search_term, cx=cse_id, **kwargs).execute()
return res['items']
results= google_search("YOUR SEARCH QUERY HERE",my_api_key,my_cse_id,num=10)
for result in results:
print(result["link"])