现在环境已配置完毕,您可以开始使用 Node.js 抓取 Google 地图的地点结果了。如上所述,要抓取 Google 地图的地点结果,您需要先设置 data 参数。既然所有内容都已准备就绪,您可以通过以下方式实现:
const data_id = "0x87c0ef253b04093f:0xafdfd6dc1d3a2b4e" // the data_id we retrieved earlier
const latitude = '38.99313451901278'
const longitude = '-94.59368586441806'
const data = '!4m5!3m4!1s' + data_id + '!8m2!3d' + latitude + '!4d' + longitude
接下来,您需要修改 options 对象,告知 API 您要获取的是地点结果。有了这个新的 data 参数,API 就能准确知道您需要抓取哪个地点的信息:
const options = {
"method": "GET",
"hostname": "serpapi.webscrapingapi.com",
"port": null,
"path": `/v1?engine=google_maps&api_key=${API_KEY}&type=place&data=${data}`, // this time the type is place and there is no query needed
"headers": {}
};
生成的脚本应如下所示:
const https = require("https");
const API_KEY = "<YOUR-API-KEY-HERE>" // You can get by creating an account - https://app.webscrapingapi.com/register
const data_id = "0x87c0ef253b04093f:0xafdfd6dc1d3a2b4e" // the data_id we retrieved earlier
const latitude = '38.99313451901278'
const longitude = '-94.59368586441806'
const data = '!4m5!3m4!1s' + data_id + '!8m2!3d' + latitude + '!4d' + longitude
const options = {
"method": "GET",
"hostname": "serpapi.webscrapingapi.com",
"port": null,
"path": `/v1?engine=google_maps&api_key=${API_KEY}&type=place&data=${data}`, // this time the type is place and there is no query needed
"headers": {}
};
const req = https.request(options, function (res) {
const chunks = [];
res.on("data", function (chunk) {
chunks.push(chunk);
});
res.on("end", function () {
const body = Buffer.concat(chunks);
const response = JSON.parse(body.toString());
console.log(response)
});
});
req.end();
运行此脚本后,您将收到如下响应:
place_results: {
title: 'Waldo Pizza',
data_id: '0x89c259a61c75684f:0x79d31adb123348d2',
place_id: 'ChIJT2h1HKZZwokR0kgzEtsa03k',
data_cid: '8778389626880739538',
website: 'https://www.stumptowntogo.com/',
gps_coordinates: { latitude: 38.99313451901278, longitude: -94.59368586 },
reviews_link: 'https://serpapi.webscrapingapi.com/v1?engine=google_maps_reviews&data_id=0x89c259a61c75684f:0x79d31adb123348d2',
place_id_search: 'https://serpapi.webscrapingapi.com/v1?engine=google_maps&type=place&device=desktop&data=!4m5!3m4!1s0x89c259a61c75684f:0x79d31adb123348d2!8m2!3d38.99313451901278!4d-94.59368586',
thumbnail: 'https://lh5.googleusercontent.com/p/AF1QipNtnPBJ2Oi_C2YNamHTXyqU9I8mRBarCIvM5g5v=w408-h272-k-no',
rating: 4.6,
reviews: 2594,
price: '$$',
type: [ 'Pizza restaurant' ],
service_options: { dine_in: true, curbsidepickup: true, no_contactdelivery: true },
extensions: [
[Object], [Object],
[Object], [Object],
[Object], [Object],
[Object], [Object],
[Object], [Object]
],
open_state: 'Closed',
hours: [
[Object], [Object],
[Object], [Object],
[Object], [Object],
[Object]
],
contact_details: {
address: [Object],
action_1: [Object],
menu: [Object],
phone: [Object],
plus_code: [Object]
},
address: '7433 Broadway St, Kansas City, MO 64114',
images: [
[Object], [Object],
[Object], [Object],
[Object], [Object],
[Object], [Object],
[Object], [Object],
[Object], [Object]
],
people_also_search_for: [ [Object], [Object], [Object] ],
user_reviews: { summaries: [Array], most_relevant: [Array] },
popular_times: { graph_results: [Object] }
}
}
就这样。这意味着您已成功使用我们的 API 抓取了 Google 地图地点结果,现在可以将获取的数据用于多种不同用途,例如数据分析、商业分析、机器学习等。如需进一步参考以及其他 6 种编程语言的代码示例以助您入门,欢迎查阅我们的 Google 地图文档。