使用puppeteer.js刮kijiji汽车.无法将不同div的值全部放在一个位置



这个标题很令人困惑,但我不知道该怎么问才好。

我本质上是想同时获得页面上每个列表的标题和价格,并将它们存储在一个地方。

我当前的代码如下:

const puppeteer = require("puppeteer");
function run() {
return new Promise(async (resolve, reject) => {
try {
const browser = await puppeteer.launch({
headless: false,
slowMo: 250,
devtools: true,
});
const page = await browser.newPage();
await page.setViewport({ width: 1280, height: 800 });
await page.goto(
"https://www.kijiji.ca/b-cars-vehicles/city-of-toronto/honda-civic/k0c27l1700273",
{ waitUntil: "networkidle2" }
);
let urls = await page.evaluate(() => {
let results = [];
// to get the titles 
let titles = document.querySelectorAll(
"div.regular-ad > div.clearfix > div.info > div.info-container > div.title"
);
titles.forEach((title) => {
results.push({
title: title.innerText,
});
});
// to get the price
let prices = document.querySelectorAll(
"div.regular-ad > div.clearfix > div.info > div.info-container > div.price"
);
prices.forEach((price) => {
results.push({
price: price.innerText,
});
});
return results;
});
browser.close();
return resolve(urls);
} catch (e) {
return reject(e);
}
});
}
run().then(console.log).catch(console.error);

然而,我的输出是在不同的对象中输出的——首先是所有标题的数组,然后是所有价格的数组,我想在同一个对象中进行标题和价格匹配,然后转到下一个对象!

应该看起来像:

page.evaluate(() => {
return [...document.querySelectorAll('div.info-container')].map(div => {
return {
title: div.querySelector('.title').innerText,
pricee: div.querySelector('.price').innerText
}
})
})

相关内容

最新更新