我用node.js和puppeteer结合编写了一个脚本,将links解析为网页中的所有帖子标题,并使用这些links导航到其内部页面以刮取标题。
我本可以从它的登陆页面抓取标题,但我的意图是使用那些新填充的链接导航,并从目标页面解析标题。当我执行我的脚本时,它会刮掉第一个标题,然后抛出一个错误。按照我试图应用的逻辑,如何使它成功。
Link to one of such target pages
到目前为止,这是我的剧本:
const puppeteer = require("puppeteer");
(async function main() {
const browser = await puppeteer.launch({headless:false});
const page = await browser.newPage();
await page.goto("https://stackoverflow.com/questions/tagged/web-scraping?sort=newest&pageSize=50");
page.waitForSelector(".summary");
const sections = await page.$$(".summary");
for (const section of sections) {
const itemName = await section.$eval(".question-hyperlink", item => item.href);
(async function main() {
await page.goto(itemName);
page.waitForSelector(".summary");
const titles = await page.$$("#question-header");
for (const title of titles) {
const itmName = await title.$eval("#question-header .question-hyperlink", itm => itm.innerText);
console.log(itmName);
}
})();
}
browser.close();
})();我在控制台上看到的是:
(node:1992) UnhandledPromiseRejectionWarning: Error: Execution context was destroyed, most likely because of a navigation.
at rewriteError (c:\Users\WCS\node_modules\puppeteer\lib\ExecutionContext.js:144:15)
at <anonymous>
at process._tickCallback (internal/process/next_tick.js:189:7)
(node:1992) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)
(node:1992) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
How to search content related to keyword in an website?
(node:1992) UnhandledPromiseRejectionWarning: TimeoutError: waiting for selector ".summary" failed: timeout 30000ms exceeded
at new WaitTask (c:\Users\WCS\node_modules\puppeteer\lib\FrameManager.js:862:28)
at Frame._waitForSelectorOrXPath (c:\Users\WCS\node_modules\puppeteer\lib\FrameManager.js:753:12)
at Frame.waitForSelector (c:\Users\WCS\node_modules\puppeteer\lib\FrameManager.js:711:17)
at Page.waitForSelector (c:\Users\WCS\node_modules\puppeteer\lib\Page.js:1043:29)
at main (c:\Users\WCS\scrape.js:15:18)
at <anonymous>
at process._tickCallback (internal/process/next_tick.js:189:7)
(node:1992) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 2)你可以看到,我在错误中得到了一个结果。
发布于 2018-10-24 12:20:01
解决你的问题有两种方法:
First:创建一个URL数组来遍历,然后重用page来访问它们。
const puppeteer = require("puppeteer");
(async function main() {
const browser = await puppeteer.launch({headless:false});
const page = await browser.newPage();
await page.goto("https://stackoverflow.com/questions/tagged/web-scraping?sort=newest&pageSize=50", {waitUntil: 'networkidle2'});
await page.waitForSelector(".summary");
const urls = await page.$$eval(".question-hyperlink", items => items.map(item => item.href));
console.log(urls);
for (let url of urls)
{
await page.goto(url);
await page.waitForSelector("#question-header");
let title = await page.$eval("#question-header a", item => item.textContent);
console.log(title);
}
await browser.close();
})();第二:正如罗曼所建议的那样,创建另一个页面并使用它来迭代页面。
下面是您的脚本的副本,实现了方法2,还纠正了另外几个问题(缺少await操作符,问题页上的选择器不正确)
const puppeteer = require("puppeteer");
(async function main() {
const browser = await puppeteer.launch({headless:false});
const page = await browser.newPage();
const newPage = await browser.newPage();
await page.goto("https://stackoverflow.com/questions/tagged/web-scraping?sort=newest&pageSize=50", {waitUntil: 'networkidle2'});
await page.waitForSelector(".summary");
const sections = await page.$$(".summary");
for (const section of sections) {
let itemURL = await section.$eval(".question-hyperlink", item => item.href);
await newPage.goto(itemURL);
await newPage.waitForSelector("#question-header"); // <-- was ".summary"
let titles = await newPage.$$("#question-header");
for (let title of titles) {
let itmName = await title.$eval("#question-header .question-hyperlink", itm => itm.innerText);
console.log(itmName);
}
}
await browser.close();
})();发布于 2018-10-24 11:36:17
我没有重播这个场景,但是您的两个错误来自:
await前面丢失了两个page.waitForSelector(".summary");page.goto()从上下文中导航,然后尝试计算不再属于DOM的section对象上的内容。要解决第一个问题,只需添加两个丢失的await。
要解决第二个问题,请使用let newPage = await browser.newPage()和newPage.goto('whereveryouwanttogo.com')打开一个新页面。这样,您就不会破坏原来的page,并且仍然可以执行section操作。
https://stackoverflow.com/questions/52967604
复制相似问题