我实际上正在研究类似于 JARVIS 的人工智能。我想从谷歌中获取答案,这样当我向我的人工智能提出问题时;它会说出问题的答案。例如,如果我在 Google 中搜索“Google 属于哪个国家/地区?”谷歌只说“加利福尼亚”。我尝试过使用 Google 模块来使用此类提取信息:
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()
CSS
选择器。这将变成:
# https://www.crummy.com/software/BeautifulSoup/bs4/doc/#css-selectors
answer = soup.select_one('.IZ6rdc').text
会变成这样:
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
或者,您可以通过使用 SerpApi 中的 Google 搜索引擎结果 API 来实现相同的效果。它是一个带有免费计划的付费 API。使用您的搜索查询检查playground。
要集成的代码:
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
免责声明,我为 SerpApi 工作。
有人可以帮助我尝试让我的人工智能助手对我做出回应吗?我已经尝试过多次了。这就是我所拥有的:
def interact():
while true:
res = obj.user_input
return({responses})
(input("Hello there. I'm Sofia, here to help!"))
请说完。我正在使用在线Python。