For educational purposes I am trying to get the reviews of this page https://www.tripadvisor.es/Restaurant_Review-g294308-d4754017-Reviews-or10-TAC_ROLL-Quito_Pichincha_Province.html . I have 10 reviews per page and I have these set of html selectors (my code used to get all the 10 comments from every page but the page was updated):
#review_593124597 > div:nth-child(1) > div:nth-child(2) > div:nth-child(5) > div:nth-child(1) > p:nth-child(1)
#review_583146930 > div:nth-child(1) > div:nth-child(2) > div:nth-child(4) > div:nth-child(1) > p:nth-child(1)
#review_577877496 > div:nth-child(1) > div:nth-child(2) > div:nth-child(4) > div:nth-child(1) > p:nth-child(1)
#review_572957932 > div:nth-child(1) > div:nth-child(2) > div:nth-child(4) > div:nth-child(1) > p:nth-child(1)
#review_571417105 > div:nth-child(1) > div:nth-child(2) > div:nth-child(5) > div:nth-child(1) > p:nth-child(1)
#review_565883882 > div:nth-child(1) > div:nth-child(2) > div:nth-child(5) > div:nth-child(1) > p:nth-child(1)
#review_564612180 > div:nth-child(1) > div:nth-child(2) > div:nth-child(4) > div:nth-child(1) > p:nth-child(1)
#review_554301618 > div:nth-child(1) > div:nth-child(2) > div:nth-child(4) > div:nth-child(1) > p:nth-child(1)
The 2 things that change is the review id and the 4th div (going between nth-child 4 and 5, I dont know if these also affects the result of the innerText ). I am trying to get the innerText of these elements but I have no luck. The code I am currently using is:
const comentarios = 'div[id^=review_] > div:nth-child(1) > div:nth-child(2) > div:nth-child(5) > div:nth-child(1) > p:nth-child(1)'
const comnetarioLength = 'partial_entry';
let listLength = await page.evaluate((sel) => {
window.scrollBy(0, window.innerHeight);
return document.getElementsByClassName(sel).length;
}, comnetarioLength);
console.log(listLength);
The following is my old code that used to work but the page was updated and I dont know what exactly I have to do, since I only get the first innerText of every page :
for (let i = 1; i <= listLength; i++) {
let selectorComentarios = comentarios.replace("Index", i); //<--I know
//this is supposed to be different
let comentario = await page.evaluate((sel) => { // Let's create variables and store values...
try {
let comentarioText = document.querySelector(sel).innerText;
return comentarioText;
}
catch (e) { }
}, selectorComentarios);
console.log(comentario);
}
Something like this? This script outputs an array with the first 10 reviews.
'use strict';
const puppeteer = require('puppeteer');
(async function main() {
try {
const browser = await puppeteer.launch();
const [page] = await browser.pages();
await page.goto('https://www.tripadvisor.es/Restaurant_Review-g294308-d4754017-Reviews-or10-TAC_ROLL-Quito_Pichincha_Province.html');
const reviews = await page.evaluate(
() => [...document.querySelectorAll('p.partial_entry')]
.map( ({ innerText }) => innerText )
)
console.log(reviews);
await browser.close();
} catch (err) {
console.error(err);
}
})();
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.