我正在寻找要在 Node.js 中使用的 Google 搜索 API 包装器,我已经四处搜索,但没有找到更新和完全烘焙的东西。任何人都可以推荐一些有用的东西吗?谢谢
5 回答
你为什么不为谷歌 API 使用节点客户端库?https://github.com/google/google-api-nodejs-client
var googleapis = require('googleapis');
googleapis.discover('customsearch', 'v1').execute(function(err, client) {
// set api key
client.withApiKey('...');
client.search.cse.list({ q: '...' }).execute(console.log);
});
我假设您不是指已弃用的 Google Web Search API ...
Google 自定义搜索 API是一个 RESTful API。这意味着您无需专门的包装器即可轻松访问它。
有几个模块可以使这更容易。我通常使用的是request模块,它可以让您非常简单地发出 HTTP 请求。
我刚刚使用了 node-google-images,它在不到 2 分钟的时间内立即生效:
https://github.com/vdemedes/node-google-images
打电话
npm install google-images
接着
client = require( 'google-images' );
client.search( 'Chicken Teriyaki', function (err, images) {
console.log(images)
});
将返回
[{宽度:'1920',高度:'1280',网址:' http ://www.springkitchenrestaurant.com/Chicken_Teriyaki.jpg',writeTo:[功能]}]
(实际上,它将返回 4 个结果,但 stackoverflow 阻止我发布超过 2 个链接...... - 你明白了要点!)
您可以使用 jsearch 模块。安装:
npm install jsearch
用法:
js.google('queryStringYouWant',10,function(response){
console.log(response) // for Google results
})
您可以使用SerpApi等第三方服务及其Node.js 库来抓取 Google 并获取结构化 JSON。
要安装它:
npm install google-search-results-nodejs
代码示例:
var gsr = require('GoogleSearchResults')
let serp = new gsr.GoogleSearchResults()
query_params = {
q: "query",
google_domain: "Google Domain",
location: "Location Requested",
device: device,
hl: "Google UI Language",
gl: "Google Country",
safe: "Safe Search Flag",
num: "Number of Results",
start: "Pagination Offset",
serp_api_key: "demo"
}
callback = function(data) {
console.log(data)
}
// Show result as JSON
serp.json(query_params, callback)
// Show results as JSON with Images
serp.jsonWithImages(query_params, callback)
// Show result as HTML file
serp.html(query_params, callback)