如何使用puppeteer从谷歌地图获取所有评论/评论? (我不会得到所有这些,因为页面是可滚动的)
我试图从我用Puppeteer搜索的地方搜集评论/评论。我有两个问题:
- 我只从当前页面获得16条评论/评论,实际上我想要所有评论/评论(在这种情况下62条评论甚至更多,取决于我的搜索)但我认为问题来自页面可滚动。
- 当我刮掉谷歌地图中没有评论的评论时,我收到一个错误:
"(node:13184) UnhandledPromiseRejectionWarning: Error: Evaluation failed: TypeError: Cannot read property 'innerText' of null at __puppeteer_evaluation_script__:9:38"
,我不知道如何每次有一个评论都有一个NULL注释(我有一些代码几乎在最后试图解决NULL注释但不起作用,我不知道如何摆脱它,我尝试了一些其他的方法,也没有工作)。
以下是我的代码:
const puppeteer = require('puppeteer'); // Require the Package we need...
let scrape = async () => { // Prepare scrape...
const browser = await puppeteer.launch({args: ['--no-sandbox', '--disabled-setuid-sandbox']}); // Prevent non-needed issues for *NIX
const page = await browser.newPage(); // Create request for the new page to obtain...
const busqueda = 'Alitas+del+Cadillac+Tumbaco';
const Url = `/${busqueda}`;
const buscar = '.section-result';
const click1 = '.widget-pane-link';
const cajaTexto = '#searchboxinput';
const comentarioLength = 'section-review-text';
const comentarios = 'div.section-review:nth-child(Index) > div:nth-child(1) > div:nth-child(3) > div:nth-child(2) > div:nth-child(1) > span:nth-child(4)';
console.log(comentarioLength);
//const comentario = 'div.section-review:nth-child(INDEX) > div:nth-child(1) > div:nth-child(3) > div:nth-child(2) > div:nth-child(1) > span:nth-child(4)';
// Replace with your Google Maps URL... Or Test the Microsoft one...
//await page.goto('/@36.1275216,-115.1728651,17z/data=!3m1!5s0x80c8c416a26be787:0x4392ab27a0ae83e0!4m7!3m6!1s0x80c8c4141f4642c5:0x764c3f951cfc6355!8m2!3d36.1275216!4d-115.1706764!9m1!1b1');
await page.goto(Url); // Define the Maps URL to Scrape...
await page.waitFor(2*1000); // In case Server has JS needed to be loaded...
await page.click(buscar); //busco caja de texto*/
await page.waitForNavigation();
await page.waitFor(2*1000);
await page.click(click1);
await page.waitForNavigation();
await page.waitFor(2*1000);
console.log(page.url());
console.log("3");
await page.evaluate(_ => { // This is just a test, don't really need this!
});
await page.waitFor(2*1000);
console.log('how many?', (await page.$$('.section-review-text')).length);
//div.section-result:nth-child(1) > div:nth-child(1) > div:nth-child(1) > div:nth-child(1) > div:nth-child(2) > h3:nth-child(1) > span:nth-child(1)
let listLength = await page.evaluate((sel) => {
window.scrollBy(0, window.innerHeight);
return document.getElementsByClassName(sel).length;
}, comentarioLength);
console.log(listLength);
for (let i = 1; i <= listLength; i++) {
let selectorComentarios = comentarios.replace("Index", i);
const result = await page.evaluate((sel) => { // Let's create variables and store values...
return document.querySelector(sel).innerText;
}, selectorComentarios);
if(!result){
continue;
}
console.log(i+result);
}
/*await page.evaluate(_ => {
window.scrollBy(0, window.innerHeight)
})*/
browser.close(); // Close the Browser...
return result; // Return the results with the Review...
};
scrape().then((value) => { // Scrape and output the results...
console.log(value); // Yay, output the Results...
});
回答如下:
这违反了Google Maps Platform的服务条款。
请参阅第3.2.4段(禁止滥用服务的限制)。它读
(a)没有刮痧。客户不得提取,导出,删除或缓存Google地图内容,以便在服务之外使用。例如,客户不会:(i)在服务之外预取,索引,存储,转发或重新托管Google地图内容; (ii)批量下载地理编码; (iii)复制商业名称,地址或用户评论;或(iv)使用Google地图内容提供文字转语音服务。某些服务允许缓存,如地图服务特定条款中所述。
来源:https://cloud.google/maps-platform/terms/#3-license
很抱歉收到坏消息。
如何使用puppeteer从谷歌地图获取所有评论/评论? (我不会得到所有这些,因为页面是可滚动的)
我试图从我用Puppeteer搜索的地方搜集评论/评论。我有两个问题:
- 我只从当前页面获得16条评论/评论,实际上我想要所有评论/评论(在这种情况下62条评论甚至更多,取决于我的搜索)但我认为问题来自页面可滚动。
- 当我刮掉谷歌地图中没有评论的评论时,我收到一个错误:
"(node:13184) UnhandledPromiseRejectionWarning: Error: Evaluation failed: TypeError: Cannot read property 'innerText' of null at __puppeteer_evaluation_script__:9:38"
,我不知道如何每次有一个评论都有一个NULL注释(我有一些代码几乎在最后试图解决NULL注释但不起作用,我不知道如何摆脱它,我尝试了一些其他的方法,也没有工作)。
以下是我的代码:
const puppeteer = require('puppeteer'); // Require the Package we need...
let scrape = async () => { // Prepare scrape...
const browser = await puppeteer.launch({args: ['--no-sandbox', '--disabled-setuid-sandbox']}); // Prevent non-needed issues for *NIX
const page = await browser.newPage(); // Create request for the new page to obtain...
const busqueda = 'Alitas+del+Cadillac+Tumbaco';
const Url = `/${busqueda}`;
const buscar = '.section-result';
const click1 = '.widget-pane-link';
const cajaTexto = '#searchboxinput';
const comentarioLength = 'section-review-text';
const comentarios = 'div.section-review:nth-child(Index) > div:nth-child(1) > div:nth-child(3) > div:nth-child(2) > div:nth-child(1) > span:nth-child(4)';
console.log(comentarioLength);
//const comentario = 'div.section-review:nth-child(INDEX) > div:nth-child(1) > div:nth-child(3) > div:nth-child(2) > div:nth-child(1) > span:nth-child(4)';
// Replace with your Google Maps URL... Or Test the Microsoft one...
//await page.goto('/@36.1275216,-115.1728651,17z/data=!3m1!5s0x80c8c416a26be787:0x4392ab27a0ae83e0!4m7!3m6!1s0x80c8c4141f4642c5:0x764c3f951cfc6355!8m2!3d36.1275216!4d-115.1706764!9m1!1b1');
await page.goto(Url); // Define the Maps URL to Scrape...
await page.waitFor(2*1000); // In case Server has JS needed to be loaded...
await page.click(buscar); //busco caja de texto*/
await page.waitForNavigation();
await page.waitFor(2*1000);
await page.click(click1);
await page.waitForNavigation();
await page.waitFor(2*1000);
console.log(page.url());
console.log("3");
await page.evaluate(_ => { // This is just a test, don't really need this!
});
await page.waitFor(2*1000);
console.log('how many?', (await page.$$('.section-review-text')).length);
//div.section-result:nth-child(1) > div:nth-child(1) > div:nth-child(1) > div:nth-child(1) > div:nth-child(2) > h3:nth-child(1) > span:nth-child(1)
let listLength = await page.evaluate((sel) => {
window.scrollBy(0, window.innerHeight);
return document.getElementsByClassName(sel).length;
}, comentarioLength);
console.log(listLength);
for (let i = 1; i <= listLength; i++) {
let selectorComentarios = comentarios.replace("Index", i);
const result = await page.evaluate((sel) => { // Let's create variables and store values...
return document.querySelector(sel).innerText;
}, selectorComentarios);
if(!result){
continue;
}
console.log(i+result);
}
/*await page.evaluate(_ => {
window.scrollBy(0, window.innerHeight)
})*/
browser.close(); // Close the Browser...
return result; // Return the results with the Review...
};
scrape().then((value) => { // Scrape and output the results...
console.log(value); // Yay, output the Results...
});
回答如下:
这违反了Google Maps Platform的服务条款。
请参阅第3.2.4段(禁止滥用服务的限制)。它读
(a)没有刮痧。客户不得提取,导出,删除或缓存Google地图内容,以便在服务之外使用。例如,客户不会:(i)在服务之外预取,索引,存储,转发或重新托管Google地图内容; (ii)批量下载地理编码; (iii)复制商业名称,地址或用户评论;或(iv)使用Google地图内容提供文字转语音服务。某些服务允许缓存,如地图服务特定条款中所述。
来源:https://cloud.google/maps-platform/terms/#3-license
很抱歉收到坏消息。