如何使用Python在Google或DuckDuckGo中快速获得答案

2024-06-16 09:58:59 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个人工智能助理项目,我想它在互联网上搜索。我想使用Google Quick Answer Box或DuckDuckGo即时回答API for Python。我看到了其他问题,但它们对我帮助不大。以下是我想要实现的一个示例:

问题:什么是长颈鹿

谷歌的答案是:

enter image description here

Duckgo的回答是:

enter image description here

如你所见,答案从

'The giraffe is an African artiodactyl mammal...'

如何使用Python获取此文本?(让我举个例子,“什么是长颈鹿”。我想用这个方法几乎所有的东西,比如“告诉我美国总统”等等。)


Tags: the项目答案answerboxapi示例for
1条回答
网友
1楼 · 发布于 2024-06-16 09:58:59

您可以使用注释中建议的duckduckgo API,方法如下:

GET https://api.duckduckgo.com?q=[your query]&format=json

下面是一个使用的示例:

import requests

query = "What is giraffe?"

r = requests.get("https://api.duckduckgo.com",
    params = {
        "q": query,
        "format": "json"
    })

data = r.json()

print(data)

print("Abstract")
print(data["Abstract"])

输出:

The giraffe is an African artiodactyl mammal, the tallest living terrestrial animal and the largest ruminant. ..........

相关问题 更多 >