Reputation: 71
I am actually working on an AI similar to JARVIS. I want to scrape answers from Google so that when I ask a question from my AI; it will speak the answer to the question. For example, if I search Google "Google belongs to which country?" Google simply speaks 'California'. I have tried a Google module to extract the information using this class:
class Gsearch_python:
def __init__(self,name_search):
self.name = name_search
def Gsearch(self):
count = 0
try :
from googlesearch import search
except ImportError:
print("No Module named 'google' Found")
for i in search(query=self.name,tld='co.in',lang='en',num=10,stop=1,pause=2):
count += 1
print (count)
print(i + '\n')
gs = Gsearch_python('google belongs to which country')
gs.Gsearch()
Upvotes: 1
Views: 2309
Reputation: 1724
CSS
selector from SelectorGadget in your code.Which will become:
# https://www.crummy.com/software/BeautifulSoup/bs4/doc/#css-selectors
answer = soup.select_one('.IZ6rdc').text
And which will become this:
from bs4 import BeautifulSoup
import requests
headers = {
'User-agent':
'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/101.0.4951.67 Safari/537.36'
}
html = requests.get('https://www.google.com/search?q="Google belongs to which country?', headers=headers)
soup = BeautifulSoup(html.text, 'html.parser')
answer = soup.select_one('.IZ6rdc').text
print(answer)
# Output: United States of America
Alternatively, you can achieve the same thing by using Google Search Engine Results API from SerpApi. It's a paid API with a free plan. Check the playground with your search query.
Code to integrate:
from serpapi import GoogleSearch
params = {
"api_key": "YOUR_API_KEY",
"engine": "google",
"q": "Google belongs to which country?",
}
search = GoogleSearch(params)
results = search.get_dict()
answer = results['answer_box']['answer']
print(answer)
# Output: American
Disclaimer, I work for SerpApi.
Upvotes: 1