cheerio, express.js
Witam
Chcę przechować w zmiennej premieres m.in. informacje dot. atrybutu src elementów zdjęć. Problem polega na tym, że na stronie domyślnie jest coś w rodzaju lazy loadingu i początkowo atrybutem src jest link do placeholdera, który zmienia się dopiero po zaczytaniu strony:
https://m.media-amazon.com/images/S/sash/4FyxwxECzL-U1J8.png
W jaki sposób mogę dostać się do danych tego atrybutu dopiero, gdy zdjęcia się załadują? Mowa tu o podstronie: https://www.imdb.com/showtimes/location?ref_=inth_mny_sm&sort=release_date,desc&st_dt=2021-10-11&mode=showtimes_grid&page=1
1. crawler.js
import * as cheerio from "cheerio";
import request from "request";
const fetchPremieresData = () => {
return new Promise((resolve, reject) => {
request(
"https://www.imdb.com/showtimes/location?ref_=inth_mny_sm&sort=release_date,desc&st_dt=2021-10-11&mode=showtimes_grid&page=1",
(error, response, html) => {
let premieres = [];
if (!error && response.statusCode === 200) {
const $ = cheerio.load(html);
$("div.title").each((i, el) => {
const title = $(el).text();
premieres.push({ id: i, title });
});
$("img.loadlate").each((i, el) => {
const cover = $(el).attr("src");
premieres[i].cover = cover;
});
resolve(premieres);
console.log(JSON.stringify(premieres));
} else {
reject(error);
console.error(error);
}
}
);
});
};
export default fetchPremieresData;
2. server.js:
app.get("/premieres", (req, res) => {
const premieresData = fetchPremieresData();
try {
const showPremieres = async () => {
const data = await premieresData;
res.send(data);
};
showPremieres();
} catch (err) {
console.error(err);
}
});
3. response:
// http://localhost:1410/premieres
[
{
"id": 0,
"title": "Nędzarz i madame",
"cover": "https://m.media-amazon.com/images/S/sash/4FyxwxECzL-U1J8.png"
},
{
"id": 1,
"title": "Pitbull",
"cover": "https://m.media-amazon.com/images/S/sash/4FyxwxECzL-U1J8.png"
},
{
"id": 2,
"title": "To musi być miłość",
"cover": "https://m.media-amazon.com/images/S/sash/4FyxwxECzL-U1J8.png"
},
{
"id": 3,
"title": "Cuidado con lo que deseas",
"cover": "https://m.media-amazon.com/images/S/sash/4FyxwxECzL-U1J8.png"
},
{
"id": 4,
"title": "Pan de limón con semillas de amapola",
"cover": "https://m.media-amazon.com/images/S/sash/4FyxwxECzL-U1J8.png"
},
{
"id": 5,
"title": "Furioza",
"cover": "https://m.media-amazon.com/images/S/sash/4FyxwxECzL-U1J8.png"
},
{
"id": 6,
"title": "Eternals",
"cover": "https://m.media-amazon.com/images/S/sash/4FyxwxECzL-U1J8.png"
},
{
"id": 7,
"title": "Poroże",
"cover": "https://m.media-amazon.com/images/S/sash/4FyxwxECzL-U1J8.png"
},
{
"id": 8,
"title": "Pogromcy duchów. Dziedzictwo",
"cover": "https://m.media-amazon.com/images/S/sash/4FyxwxECzL-U1J8.png"
},
...