I am trying to get the last movie/series I rated on Letterboxhd by using XPath, then print it. To get the first movie, I found this in HTML: "<span class="frame-title">Magnolia (1999)</span>".
To get the rating of the first movie: "<span class="rating -tiny -darker rated-6">★★★</span>"
I know that every time I run this code the rating will be 3 stars, so I only wrote half of it.
Here is what I did:
let data = await page.evaluate(() => {
let titles = document.evaluate("//span[contains(@class, 'frame-title')]", document, null, XPathResult.ANY_TYPE, null);
let title = titles.iterateNext();
let ratings = document.evaluate("//span[contains(@class, ' -tiny')]", document, null, XPathResult.ANY_TYPE, null);
let rating = ratings.iterateNext();
return{
title,
rating
}
});
When I run this code, I see "data" is undefined. What am I doing wrong? What should I do instead?
Here is my entire code:
const puppeteer = require('puppeteer');
(async () => {
let movieUrl = 'https://letterboxd.com/sdeer/films/';
let browser = await puppeteer.launch({ headless: true });
let page = await browser.newPage();
await page.goto(movieUrl, { waitUntil: 'networkidle2'});
let data = await page.evaluate(() => {
let titles = document.evaluate("//span[contains(@class, 'frame-title')]", document, null, XPathResult.ANY_TYPE, null);
let title = titles.iterateNext();
let ratings = document.evaluate("//span[contains(@class, ' -tiny')]", document, null, XPathResult.ANY_TYPE, null);
let rating = ratings.iterateNext();
return{
title,
rating
}
});
debugger
console.log(data.title.textContent);
console.log(data.rating.textContent);
await browser.close();
})();