环境配置完成后,您已准备好使用我们的 API 开始抓取 Google 地图评论。要继续操作,您需要按照之前提到的方法设置 data 参数。在获取所有必要信息后,您可以按以下方式设置 data_id 参数:
const data_id = "0x87c0ef253b04093f:0xafdfd6dc1d3a2b4e" // the data_id we retrieved earlier
现在,唯一剩下的就是修改 options 对象,从而告知我们的 API 您希望从 Google 地图抓取评论:
const options = {
"method": "GET",
"hostname": "serpapi.webscrapingapi.com",
"port": null,
"path": `/v1?engine=google_maps_reviews&api_key=${API_KEY}&data_id=${data_id}`, // there is no need in having a query anymore, data_id is enough to identify a place
"headers": {}
};
以上就是您需要做的全部操作。您的脚本现在应如下所示:
const http = require("https");
const API_KEY = "<YOUR-API-KEY-HERE>"
const data_id = "0x87c0ef253b04093f:0xafdfd6dc1d3a2b4e" // the data_id we retrieved earlier
const options = {
"method": "GET",
"hostname": "serpapi.webscrapingapi.com",
"port": null,
"path": `/v1?engine=google_maps_reviews&api_key=${API_KEY}&data_id=${data_id}`, // there is no need in having a query anymore, data_id is enough to identify a place
"headers": {}
};
const req = http.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();
执行此脚本后,您将收到类似于以下内容的响应:
reviews: [
{
link: 'https://www.google.com/maps/reviews/data=!4m8!14m7!1m6!2m5!1sChZDSUhNMG9nS0VJQ0FnSUMyem9pOEdBEAE!2m1!1s0x0:0xafdfd6dc1d3a2b4e!3m1!1s2@1:CIHM0ogKEICAgIC2zoi8GA%7CCgwI1vuBkwYQiKeWyQE%7C?hl=en-US',
date: '8 months ago',
rating: 5,
snippet: 'Wow, if you have dietary restrictions this is absolutely the place to go! Both for the variety of restrictions they cater to as well as the taste of the dishes.The good: great tasting food. Very conscious of dietary restrictions which include multiple types of vegan cheeses as well as gluten free. Decent drink selection.The meh: service is nice but a touch slow. Maybe understaffed? Prices are average for pizzas.The bad: noneFeatures: Did not see any masks on anyone inside. Unsure of cleaning practices so I cannot speak to that.Dine in: Yes\n' +
'Takeout: Yes\n' +
'Curbside pickup: YesWow, if you have dietary restrictions this is absolutely the place to go! Both for the variety of restrictions they cater to as well as the taste of the dishes. ...More',
likes: 3,
user: [Object],
images: [Array]
},
{
link: 'https://www.google.com/maps/reviews/data=!4m8!14m7!1m6!2m5!1sChZDSUhNMG9nS0VJQ0FnSURXOUxHSUl3EAE!2m1!1s0x0:0xafdfd6dc1d3a2b4e!3m1!1s2@1:CIHM0ogKEICAgIDW9LGIIw%7CCgwI3OnIkQYQwLGL1gM%7C?hl=en-US',
date: '9 months ago',
rating: 5,
snippet: "We love Waldo Pizza! We have dairy allergies and Waldo offers a wide range of vegan cheeses as well as a ton of different toppings. The vegan dessert here is always excellent as well, super rich in flavor. Of course the traditional pizza, pasta and dessert are also amazing! It's great to have both options under one roof!Dine in: Yes\n" +
'Outdoor seating: No ...More',
likes: 1,
user: [Object],
images: [Array]
}
. . .
]
就这样!您已成功通过我们的 API 抓取了 Google 地图评论,现在可以将获取的数据用于数据分析、商业分析、机器学习等多种用途。如需进一步参考以及其他 6 种编程语言的代码示例,请查阅我们的 Google 地图评论文档。