MIT*_*THU 2 node.js web-scraping puppeteer
我已经使用 puppeteer 在节点中编写了小脚本,以从网站的登陆页面循环执行不同帖子的链接的点击。
我的脚本中使用的站点链接是一个占位符。而且,它们不是动态的。所以,傀儡师可能有点过分了。然而,我的目的是学习点击的逻辑。
当我执行第一个脚本时,它单击一次并在退出源时抛出以下错误。
const puppeteer = require("puppeteer");
(async () => {
const browser = await puppeteer.launch({headless:false});
const [page] = await browser.pages();
await page.goto("/sf/ask/tagged/web-scraping/",{waitUntil:'networkidle2'});
await page.waitFor(".summary");
const sections = await page.$$(".summary");
for (const section of sections) {
await section.$eval(".question-hyperlink", el => el.click())
}
await browser.close();
})();
Run Code Online (Sandbox Code Playgroud)
上述脚本遇到的错误:
(node:9944) UnhandledPromiseRejectionWarning: Error: Execution context was destroyed, most likely because of a navigation.
Run Code Online (Sandbox Code Playgroud)
当我执行以下命令时,脚本假装单击一次(实际上并非如此)并遇到与之前相同的错误。
const puppeteer = require("puppeteer");
(async () => {
const browser = await puppeteer.launch({headless:false});
const [page] = await browser.pages();
await page.goto("/sf/ask/tagged/web-scraping/");
await page.waitFor(".summary .question-hyperlink");
const sections = await page.$$(".summary .question-hyperlink");
for (let i=0, lngth = sections.length; i < lngth; i++) {
await sections[i].click();
}
await browser.close();
})();
Run Code Online (Sandbox Code Playgroud)
上面的错误抛出:
(node:10128) UnhandledPromiseRejectionWarning: Error: Execution context was destroyed, most likely because of a navigation.
Run Code Online (Sandbox Code Playgroud)
如何让我的脚本循环执行点击?
执行上下文被破坏,很可能是因为导航。
该错误表明您想要单击某个链接,或者在某个不再存在的页面上执行某些操作,很可能是因为您导航离开了。
将 puppeteer 脚本视为浏览真实页面的真人。
首先,我们加载网址(/sf/ask/tagged/web-scraping/)。
接下来,我们要浏览该页面上提出的所有问题。为此,我们通常会做什么?我们会执行以下任一操作,
因此,它们都涉及离开和返回当前页面。
如果您不遵循此流程,您将收到上述错误消息。
至少有 4 种或更多的方法可以解决这个问题。我会选择最简单和最复杂的。
首先我们提取当前页面上的所有链接。
const links = await page.$$eval(".hyperlink", element => element.href);
Run Code Online (Sandbox Code Playgroud)
这给了我们一个 url 列表。我们可以为每个链接创建一个新选项卡。
for(let link of links){
const newTab = await browser.newPage();
await newTab.goto(link);
// do the stuff
await newTab.close();
}
Run Code Online (Sandbox Code Playgroud)
这将一一遍历每个链接。我们可以通过使用promise.map和各种队列库来改进这一点,但你明白了。
我们需要以某种方式存储状态,以便我们知道上次访问的是哪个链接。如果我们访问了第三个问题并返回到标签页面,则下次我们需要访问第四个问题,反之亦然。
检查以下代码。
const puppeteer = require("puppeteer");
(async () => {
const browser = await puppeteer.launch({ headless: false });
const page = await browser.newPage();
await page.goto(
`/sf/ask/tagged/web-scraping/?sort=newest&pagesize=15`
);
const visitLink = async (index = 0) => {
await page.waitFor("div.summary > h3 > a");
// extract the links to click, we need this every time
// because the context will be destryoed once we navigate
const links = await page.$$("div.summary > h3 > a");
// assuming there are 15 questions on one page,
// we will stop on 16th question, since that does not exist
if (links[index]) {
console.log("Clicking ", index);
await Promise.all([
// so, start with the first link
await page.evaluate(element => {
element.click();
}, links[index]),
// either make sure we are on the correct page due to navigation
await page.waitForNavigation(),
// or wait for the post data as well
await page.waitFor(".post-text")
]);
const currentPage = await page.title();
console.log(index, currentPage);
// go back and visit next link
await page.goBack({ waitUntil: "networkidle0" });
return visitLink(index + 1);
}
console.log("No links left to click");
};
await visitLink();
await browser.close();
})();
Run Code Online (Sandbox Code Playgroud)
编辑:有多个与此类似的问题。如果您想了解更多信息,我将参考它们。
| 归档时间: |
|
| 查看次数: |
5227 次 |
| 最近记录: |