我想要做的只是提交字符串查询(相当于手动输入搜索栏)并保存第一个结果的URL(如果有的话)。
我问了一个类似的问题(Python search scraping),并且它的答案适用于谷歌我的问题是我无法在任何地方找到Duckduckgo的搜索API地址。你能帮助我或建议其他方式吗?
答案 0 :(得分:2)
只需更改您想要搜索的内容即可。这将以JSON
输出结果https://api.duckduckgo.com/?q=<your search string>&format=json&pretty=1&no_html=1&skip_disambig=1
我认为这就是你要求的
答案 1 :(得分:1)
www.api.duckduckgo.com/?q =
Search
&安培;格式= JSON&安培;漂亮= 1
实施例: http://api.duckduckgo.com/?q=DuckDuckGo&format=json&pretty=1
响应是一个JSON对象,你可以找到第一个链接的地址:
response.Results[0].FirstURL
您可以通过将回复放入https://jsonformatter.curiousconcept.com/
来找到答案希望它有所帮助:)
答案 2 :(得分:0)
如果您也有兴趣检索富媒体搜索结果(“最近新闻”、“查询图像”、“知识图”等),则 DuckDuckGo 的非 JS 网络版本:https://duckduckgo.com/html/ 不会为您提供这个。
要获得完整的 DuckDuckGo 页面,您最好的选择是查询下一个链接:https://links.duckduckgo.com/d.js?
。您可以通过检查网络标签找到它。
这是所有结果的存储位置。
搜索查询示例:“bill Gates”: https://links.duckduckgo.com/d.js?q=bill%20gates&kl=us-en&l=us-en&s=0&ct=US&ss_mkt=us&vqd=3-41771934349821924699896735607141847775-125937012480658240237583475471092551742
这里有两个必需的参数。第一个是查询 q
,第二个是搜索标记 vqd
。
vqd
标记对于每个搜索都是唯一的,并且与查询相关联,因此您将无法在不同的查询中重复使用该标记。另外,我认为它的过期时间为 48 小时。
我确实尝试过对令牌进行逆向工程,但据我所知,它是在服务器端生成的,没有办法做到这一点。
还有一个值得一提的参数是s
,它用于分页。它定义了结果偏移量,并跳过给定数量的结果。
所以检索结果的流程是:
vqd
搜索令牌vqd
令牌构建请求 URL:https://links.duckduckgo.com/d.js
或者,您可以使用像 SerpApi 这样的第三方解决方案,它可以为您完成所有这些工作,甚至更多。
示例 Python 代码(也可在其他库中使用):
from serpapi import GoogleSearch
params = {
"api_key": "secret_api_key",
"engine": "duckduckgo",
"q": "bill gates",
"kl": "us-en"
}
search = GoogleSearch(params)
results = search.get_dict()
示例 JSON 输出:
"organic_results": [
{
"position": 1,
"title": "Bill Gates - Wikipedia",
"link": "https://en.wikipedia.org/wiki/Bill_Gates",
"snippet": "Early life. Bill Gates was born in Seattle, Washington, on October 28, 1955. He is the son of William H. Gates Sr. (1925-2020) and Mary Maxwell Gates (1929-1994). His ancestry includes English, German, and Irish/Scots-Irish. His father was a prominent lawyer, and his mother served on the board of directors for First Interstate BancSystem and the United Way of America.",
"favicon": "https://external-content.duckduckgo.com/ip3/en.wikipedia.org.ico",
"sitelinks": [
{
"title": "Bill Gates Sr",
"link": "https://en.wikipedia.org/wiki/Bill_Gates_Sr."
},
{
"title": "Bill & Melinda Gates Foundation",
"link": "https://en.wikipedia.org/wiki/Bill_%26_Melinda_Gates_Foundation"
},
{
"title": "The World's Billionaires",
"link": "https://en.wikipedia.org/wiki/The_World%27s_Billionaires"
},
{
"title": "Bill Gates's House",
"link": "https://en.wikipedia.org/wiki/Bill_Gates%27s_house"
},
{
"title": "Mary Maxwell Gates",
"link": "https://en.wikipedia.org/wiki/Mary_Maxwell_Gates"
},
{
"title": "Paul Allen",
"link": "https://en.wikipedia.org/wiki/Paul_Allen"
}
]
},
...
],
"knowledge_graph": {
"title": "Bill Gates",
"description": "William Henry Gates III is an American business magnate, software developer, investor, author, and philanthropist. He is a co-founder of Microsoft Corporation, along with his late childhood friend Paul Allen. During his career at Microsoft, Gates held the positions of chairman, chief executive officer, president and chief software architect, while also being the largest individual shareholder until May 2014. He is considered one of the best known entrepreneurs of the microcomputer revolution of the 1970s and 1980s. Gates was born and raised in Seattle, Washington. In 1975, he and Allen founded Microsoft in Albuquerque, New Mexico. It became the world's largest personal computer software company. Gates led the company as chairman and CEO until stepping down as CEO in January 2000, succeeded by Steve Ballmer, but he remained chairman of the board of directors and became chief software architect.",
"website": "https://www.gatesnotes.com",
"thumbnail": "https://duckduckgo.com/i/45eb7625.jpg",
"facts": {
"born": "William Henry Gates III, October 28, 1955, Seattle, Washington, U.S.",
"education": "Harvard University (dropped out)",
"occupation": "Software developer, investor, entrepreneur",
...
},
...
},
"news_results": [
{
"position": 1,
"title": "Bill Gates admitted 'messing up' marriage at 'moving' Sun Valley panel, report claims",
"link": "https://news.yahoo.com/bill-gates-admitted-messing-marriage-174024577.html",
"snippet": "Microsoft co-founder Bill Gates has purportedly confessed to \"messing up\" his marriage to his ex-wife Melinda French Gates, according to a report. The New York Post reported that the tech giant made the comments during an \"off the record\" question-and-answer session at an exclusive Sun Valley conference last week.",
"source": "YAHOO!News",
"date": "20 hours ago",
"thumbnail": "https://s.yimg.com/uu/api/res/1.2/56GaZKhuq0Dq.TS8WNZL9g--~B/aD03Njg7dz0xMDI0O2FwcGlkPXl0YWNoeW9u/https://media.zenfs.com/en/the_independent_635/0b4d2e20d6c2c4ecb1565ae82180ee6a"
},
...
],
"inline_images": [
{
"position": 1,
"title": "Bill Gates says Jobs was a wizard in getting staff to keep ...",
"link": "https://www.cnbc.com/2019/07/07/bill-gates-says-jobs-was-a-wizard-in-getting-staff-to-keep-apple-alive.html",
"thumbnail": "https://tse1.mm.bing.net/th?id=OIP.5WUHRMXHSmzjh9W7HP80hQHaE8&pid=Api",
"image": "https://image.cnbcfm.com/api/v1/image/105894488-15571466884731u8a0002r.jpg?v=1557843920"
},
...
],
"related_searches": [
{
"query": "bill gates age",
"link": "https://duckduckgo.com/?q=bill%20gates%20age"
},
{
"query": "bill gates investments in china",
"link": "https://duckduckgo.com/?q=bill%20gates%20investments%20in%20china"
},
{
"query": "news about bill gates",
"link": "https://duckduckgo.com/?q=news%20about%20bill%20gates"
},
...
]
查看documentation了解更多详情。
免责声明:我在 SerpApi 工作。