Puppeteer:for 循环 - 语法问题

我的语法有什么问题?


它抛出一个错误:


Scraping failed Error: Evaluation failed: ReferenceError: getLogo is not defined

代码片段:


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

            // Getting total number of universities

            let totalUniversities = document.querySelector('ul.universities-search-result').childElementCount

            for (i = 0; i < totalUniversities; i++) {

                getLogo += document.querySelectorAll('.profile_lead > .dp > img.logo-90x90')[i].src;

                universityURL += document.querySelectorAll('a.profile.adv, a.profile.basic')[i].href;

                let x = [getLogo, universityURL];

            };

            return x;

        });

编辑 (1) 当我声明 getLogo 和 universityURL 变量时,


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

            // Getting total number of universities

            let totalUniversities = document.querySelector('ul.universities-search-result').childElementCount

            for (i = 0; i < totalUniversities; i++) {

                let getLogo += document.querySelectorAll('.profile_lead > .dp > img.logo-90x90')[i].src;

                let universityURL += document.querySelectorAll('a.profile.adv, a.profile.basic')[i].href;

                let x = [getLogo, universityURL];

            };

            return x;

        });

我收到此错误:


SyntaxError: Unexpected token '+='

    at wrapSafe (internal/modules/cjs/loader.js:1116:16)

    at Module._compile (internal/modules/cjs/loader.js:1164:27)

    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1220:10)

    at Module.load (internal/modules/cjs/loader.js:1049:32)

    at Function.Module._load (internal/modules/cjs/loader.js:937:14)

    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:71:12)

    at internal/main/run_main_module.js:17:47


Smart猫小萌
浏览 198回答 4
4回答

一只甜甜圈

你忘了初始化getLogo,就像你所做的那样totalUniversitiesconst universityAtt = await page.evaluate(() => {&nbsp; &nbsp; let getLogo;&nbsp; &nbsp; // Getting total number of universities&nbsp; &nbsp; let totalUniversities = document.querySelector('ul.universities-search-result').childElementCount&nbsp; &nbsp; for (i = 0; i < totalUniversities; i++) {&nbsp; &nbsp; &nbsp; &nbsp; getLogo += document.querySelectorAll('.profile_lead > .dp > img.logo-90x90')[i].src;&nbsp; &nbsp; &nbsp; &nbsp; universityURL += document.querySelectorAll('a.profile.adv, a.profile.basic')[i].href;&nbsp; &nbsp; &nbsp; &nbsp; let x = [getLogo, universityURL];&nbsp; &nbsp; };&nbsp; &nbsp; return x;});

隔江千里

它就在那里说;我看到你赋值但从不声明它们let const或者var请先做这样的事情:let getLogo += document.querySelectorAll('.profile_lead > .dp > img.logo-90x90')[i].src;let universityURL += document.querySelectorAll('a.profile.adv, a.profile.basic')[i].href;

跃然一笑

请尝试这样。在实际使用它之前定义变量。let testVar=0;for (i = 0; i < 10; i++) {&nbsp; &nbsp; testVar += 3;}console.log(testVar);

慕尼黑8549860

您需要定义 getLogo 变量const universityAtt = await page.evaluate(() => {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Getting total number of universities&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; let totalUniversities = document.querySelector('ul.universities-search-result').childElementCount&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for (i = 0; i < totalUniversities; i++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; let getLogo += document.querySelectorAll('.profile_lead > .dp > img.logo-90x90')[i].src;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; universityURL += document.querySelectorAll('a.profile.adv, a.profile.basic')[i].href;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; let x = [getLogo, universityURL];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; };&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return x;&nbsp; &nbsp; &nbsp; &nbsp; });
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript