循环只给我第一行(puppeteer js)

我想获取价格和卖家数据,但我只获取第一行,为什么?我正在尝试学习js,但在这里我花了很多时间,请帮助。


我从不同的站点获取代码并根据我的要求修改它,但仍然无法正常工作


Puppeteer js、nodejs


var url = 'https://www.amazon.co.uk/gp/offer-listing/3770436385/ref=olp_f_used?ie=UTF8&f_new=true&f_usedAcceptable=true&f_usedGood=true&f_used=true&f_usedLikeNew=true&f_usedVeryGood=true';


const puppeteer = require('puppeteer');


(async function main(){

    try{


        const browser = await puppeteer.launch({headless: false });

        const page = await browser.newPage();

        page.setUserAgent('Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) HeadlessChrome/67.0.3372.0 Safari/537.36');


        await page.goto(url);

        await page.waitForSelector('div.navFooterLine:nth-child(6)');


        const sections = await page.$$('.olpOffer');


        console.log(sections.length);


        for (const section of sections){


            const result = await page.evaluate(() => {

                //  let amaOffer = document.querySelector('#olpOfferList img').innerText;

                //  let amaPrice = document.querySelector('#olpOfferList img').innerText;

                let seller = document.querySelector('.a-text-bold a').innerText;

                let price = document.querySelector('.olpOfferPrice').innerText;


                return {

                //  amaOffer,

                    seller,

                    price


            }


        } )

        console.log(result);

    }


    } catch (e) {

        console.log('Our  error', e);

    }



})();


蛊毒传说
浏览 218回答 2
2回答

qq_笑_17

你基本上是这样做的:for (const section of sections) {   const result = someValue;   console.log(result);}因此,您只是result在循环中的 over 上声明了一个新变量,而不是将每个单独的结果累积到一个数组中,这样您就可以获得所有结果。不是 100% 清楚您想要结果的位置,但您至少可以将它们累积到这样的数组中:(async function main() {    try {        const browser = await puppeteer.launch({headless: false});        const page = await browser.newPage();        page.setUserAgent('Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3372.0 Safari/537.36');        await page.goto(url);        await page.waitForSelector('div.navFooterLine:nth-child(6)');        const sections = await page.$$('.olpOffer');        console.log(sections.length);        let results = [];        for (const section of sections) {            const result = await page.evaluate(() => {                //  let amaOffer = document.querySelector('#olpOfferList img').innerText;                //  let amaPrice = document.querySelector('#olpOfferList img').innerText;                let seller = document.querySelector('.a-text-bold a').innerText;                let price = document.querySelector('.olpOfferPrice').innerText;                return {                    //  amaOffer,                    seller,                    price                }            });            console.log(result);            results.push(result);        }        // see all results here        console.log(results);        return results;    } catch (e) {        console.log('Our  error', e);    }})();

互换的青春

我知道问题出在哪里。Puppeteer JS 在两个引擎中运行 javascript,两者的作用域不同,所以我们需要将section数字传递给page.evaluate亚马逊网站显示亚马逊产品的结果,选择器没有选择正确的产品,所以我将其更改为&nbsp;.olpSellerName我添加了一些改进以使脚本运行得更快。希望这能有所帮助。let url = 'https://www.amazon.co.uk/gp/offer-listing/3770436385/ref=olp_f_used?ie=UTF8&f_new=true&f_usedAcceptable=true&f_usedGood=true&f_used=true&f_usedLikeNew=true&f_usedVeryGood=true'let chromeUA = 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3372.0 Safari/537.36'const puppeteer = require('puppeteer');(async function main() {&nbsp; &nbsp; try {&nbsp; &nbsp; &nbsp; &nbsp; const browser = await puppeteer.launch({headless: false})&nbsp; &nbsp; &nbsp; &nbsp; const page = (await browser.pages())[0]&nbsp; &nbsp; &nbsp; &nbsp; page.setUserAgent(chromeUA)&nbsp; &nbsp; &nbsp; &nbsp; page.setDefaultNavigationTimeout(0)&nbsp; &nbsp; &nbsp; &nbsp; page.setRequestInterception(true)&nbsp; &nbsp; &nbsp; &nbsp; page.on('request', async request => {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if&nbsp; (&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; request.resourceType() === 'image' ||&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; request.resourceType() === 'media' ||&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; request.resourceType() === 'script' ||&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; request.resourceType() === 'stylesheet' ||&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; request.resourceType() === 'font' ||&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; request.resourceType() === 'other'&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; request.abort()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; request.continue()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; })&nbsp; &nbsp; &nbsp; &nbsp; await page.goto(url)&nbsp; &nbsp; &nbsp; &nbsp; await page.waitForSelector('div.navFooterLine:nth-child(6)')&nbsp; &nbsp; &nbsp; &nbsp; const sections = await page.$$('.olpOffer')&nbsp; &nbsp; &nbsp; &nbsp; let results = []&nbsp; &nbsp; &nbsp; &nbsp; for (let num = 0; num < sections.length; num++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; let result = await page.evaluate( (num) => {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; let seller = document.querySelectorAll('.olpSellerName')[num].innerText&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; let price = document.querySelectorAll('.olpOfferPrice')[num].innerText&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //&nbsp; amaOffer,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; seller,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; price&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }, num)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; results.push(result)&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; // see all results here&nbsp; &nbsp; &nbsp; &nbsp; console.log(results)&nbsp; &nbsp; &nbsp; &nbsp; return results&nbsp; &nbsp; } catch (e) {&nbsp; &nbsp; &nbsp; &nbsp; console.log('Our&nbsp; error', e);&nbsp; &nbsp; }})()我还有一个问题:我想为不同的亚马逊网站循环let AmazonSites = ['de','co.uk','ca'];for(AmazonSite of AmazonSites){let url = 'https://www.amazon.'+ AmazonSite +'/gp/offer-listing/'+ Asin +'/ref=olp_f_used?ie=UTF8&f_new=true&f_usedAcceptable=true&f_usedGood=true&f_used=true&f_usedLikeNew=true&f_usedVeryGood=true';await page.goto(url)因此,您可以通过 for 循环重复脚本来发出多个请求let domains = ['co.uk', 'de', 'ca']let url = 'https://www.amazon.tld/gp/offer-listing/3770436385/ref=olp_f_used?ie=UTF8&f_new=true&f_usedAcceptable=true&f_usedGood=true&f_used=true&f_usedLikeNew=true&f_usedVeryGood=true'let chromeUA = 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3372.0 Safari/537.36'const puppeteer = require('puppeteer');(async function main() {&nbsp; &nbsp; try {&nbsp; &nbsp; &nbsp; &nbsp; const browser = await puppeteer.launch({headless: false})&nbsp; &nbsp; &nbsp; &nbsp; const page = (await browser.pages())[0]&nbsp; &nbsp; &nbsp; &nbsp; page.setUserAgent(chromeUA)&nbsp; &nbsp; &nbsp; &nbsp; page.setDefaultNavigationTimeout(0)&nbsp; &nbsp; &nbsp; &nbsp; page.setRequestInterception(true)&nbsp; &nbsp; &nbsp; &nbsp; page.on('request', async request => {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if&nbsp; ( request.resourceType() === 'document' ) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; request.continue()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; request.abort()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; })&nbsp; &nbsp; &nbsp; &nbsp; for (let tld in domains) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; await page.goto(url.replace('tld', domains[tld]))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; await page.waitForSelector('div.navFooterLine:nth-child(6)')&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; const sections = await page.$$('.olpOffer')&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; let results = []&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for (let num = 0; num < sections.length; num++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; let result = await page.evaluate( (num) => {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; let seller = document.querySelectorAll('.olpSellerName')[num].innerText&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; let price = document.querySelectorAll('.olpOfferPrice')[num].innerText&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //&nbsp; amaOffer,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; seller,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; price&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }, num)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; results.push(result)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // see all results here&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; console.log (`\n\nRESULT FOR DOMAIN TLD : www.amazon.${domains[tld]}`)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; console.log(results)&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; await browser.close()&nbsp; &nbsp; } catch (e) {&nbsp; &nbsp; &nbsp; &nbsp; console.log('Our&nbsp; error', e);&nbsp; &nbsp; }})()
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript