循环浏览页面以返回所有产品链接



目标是从分页中的所有页面获取所有产品链接。到目前为止,我已经使用console.log(链接(将信息打印到控制台。然而,由于我对这个领域完全陌生,而且完全没有经验,所以我遇到了如何使用return命令将一些值传递回的问题return links

使用console.log(links),我得到一个警告:getLinks是不可迭代的

const puppeteer = require('puppeteer')
async function getLinks(){
const browser = await puppeteer.launch({headless: false, defaultViewport: null});
const page = await browser.newPage();
const url = "https://example.com/product-category?p=1&nidx"

await page.goto(url)
while(await page.$('.change-country-buttons > button:nth-child(1)')){
await page.waitForTimeout(2000);
await page.keyboard.press('ArrowDown');
await page.waitForSelector('.change-country-buttons');
await page.waitForTimeout(2000);
await page.click('.change-country-buttons > button:nth-child(1)');
await page.waitForTimeout(2000);
}
while(await page.$(".pagination .pagination--next")){
await page.waitForTimeout(2000);
await page.evaluate(() => {
document.querySelector(".pagination .pagination--next").scrollIntoView();
});
await page.waitForTimeout(1000);
await page.waitForSelector(".pagination .pagination--next")
await page.waitForTimeout(500);
await page.click('.pagination .pagination--next')
const links = await page.$$eval('.item__info > .mtc-link:nth-child(2)', (allAs) => { return allAs.map((a) => a.href) });
await page.waitForTimeout(1500);
console.log(links)
}
}
return links // Is returning links only form the first page and then the loop stops

我用Promise.all((做了一些尝试,但我并不完全清楚该怎么做。

请帮忙,温柔一点,因为我才刚刚开始学习的基础知识

您需要创建一个数组,并将每个页面上的所有头盔链接推到它上。

这对我来说测试成功了。

const puppeteer = require('puppeteer')
async function getLinks(){
const browser = await puppeteer.launch({headless: false, defaultViewport: null});
const page = await browser.newPage();
const url = "https://www.motocard.com/en/motorcycle-road-gear/helmets/precio_150-3200/full-face?p=1&nidx"

var all_links = [];

await page.goto(url);
while(await page.$('.change-country-buttons > button:nth-child(1)')){
await page.waitForTimeout(2000);
await page.keyboard.press('ArrowDown');
await page.waitForSelector('.change-country-buttons');
await page.waitForTimeout(2000);
await page.click('.change-country-buttons > button:nth-child(1)');
await page.waitForTimeout(2000);
}
while(await page.$(".pagination .pagination--next")){
await page.waitForTimeout(2000);
await page.evaluate(() => {
document.querySelector(".pagination .pagination--next").scrollIntoView();
});
await page.waitForTimeout(1000);
await page.waitForSelector(".pagination .pagination--next")
await page.waitForTimeout(500);
await page.click('.pagination .pagination--next')
const links = await page.$$eval('.item__info > .mtc-link:nth-child(2)', (allAs) => { return allAs.map((a) => a.href) });
await page.waitForTimeout(1500);
//console.log(links)
all_links.push(...links);
}
return all_links;
}
(async ()=>{
var links = await getLinks();
console.log('done');
console.log(links);
})();

最新更新