If we enter any keyword in google search box , we will get a set of suggestions from google matching the input keyword for us to use as autocomplete.
Using this keyword we can collect the suggestions from google by using BeautifulSoup.
import requests
from bs4 import BeautifulSoup
str1='Python' # search string
link = "https://suggestqueries.google.com/complete/search?output=toolbar&hl=en&q="+str1
content = requests.get(link)
soup = BeautifulSoup(content.text, "xml") # creating bs4 object
for d in soup.find_all('CompleteSuggestion'): # loop through all suggestions
print(d.suggestion['data'])
#str1='Python' # search string
str1=input("Enter keyword: ") # user input for search string
Using multiple keywords
import requests
from bs4 import BeautifulSoup
kw=['Tkinter','Python']
kw_data=[]
for s in kw:
link = "https://suggestqueries.google.com/complete/search?output=toolbar&hl=en&q="+s
content = requests.get(link)
soup = BeautifulSoup(content.text, "xml")
my_list=[]
for d in soup.find_all('CompleteSuggestion'):
#print(d.suggestion['data']) # printing the data
my_list.append(d.suggestion['data']) # adding to list
kw_data.append(my_list)
print(kw_data)
We can store the above data in a google sheet, add this part at the end of the above code.
Python- Pygsheets »