【发布时间】:2019-06-18 15:29:45
【问题描述】:
我在node 中结合puppeteer 编写了一个脚本,以刮取遍历website 中多个页面的不同机构的名称。
我的以下脚本可以从登录页面解析机构名称,然后在解析其他页面的名称时启动几次点击,最后在执行过程中的某个时刻遇到错误。
the error: TypeError: Cannot read property 'click' of undefined
at main (c:\Users\WCS\Desktop\Node vault\comments.js:18:25)
at <anonymous>
at process._tickCallback (internal/process/next_tick.js:118:7)
我使用了硬编码的for loop,因为我真的不知道让脚本继续单击下一页按钮,直到没有留下任何东西。我希望遵守任何逻辑,以便我的脚本首先查找下一页按钮。如果它找到一个,那么它将单击该按钮并重复该过程。
我试过了:
const puppeteer = require('puppeteer');
const link = "https://www.incometaxindia.gov.in/Pages/utilities/exempted-institutions.aspx";
(async function main() {
try {
const browser = await puppeteer.launch({headless:false});
const [page] = await browser.pages();
await page.goto(link);
await page.waitForSelector("h1.faqsno-heading");
for(let i = 1; i < 20; i++){
const sections = await page.$$("h1.faqsno-heading");
for (const section of sections) {
const itemName = await section.$eval("div[id^='arrowex']", el => el.innerText);
console.log(itemName);
}
const nextPage = await page.$$(".ms-paging > a");
await nextPage[i].click();
await page.waitForNavigation({waituntil:'networkidle0'});
}
await browser.close();
} catch (e) {
console.log('the error: ', e);
}
})();
顺便说一句,为了避免重复这篇文章,我必须承认我遇到过this post,但我认为我自己无法在我的脚本中实现逻辑。
【问题讨论】:
标签: node.js web-scraping puppeteer