page.Esevaluate运行数小时

发布于 2025-02-12 09:58:58 字数 490 浏览 0 评论 0原文

我试图爬网站的内容,并且大部分时间都可以使用(几分钟内完成),但有时最多需要8个小时。我已经设法查明了问题,并且在页面上发射了。以无头模式查看网站= false,它只是无限加载(单击后)。另外,如果我在该页面上手动尝试“ document.queryselector”,它对我有用。

代码如下:

    console.log("Test");
    let value = await page.evaluate((sel) => {
        let element = document.querySelector(sel);
        return element? element.innerHTML: null;
    },selector);
    console.log("Test2");

我该怎么做才能防止它运行那么长时间(我会尝试为这种情况设置某种超时系统)? 还是在此部分中代码时如何跟踪时间?这部分之后的代码立即(仅在下班后)就无法运行。

I am trying to crawl the contents of a website and it works most of the time (finishes in minutes) but sometimes it takes up to 8 hours. I've managed to pinpoint the issue and it is fired in the page.evaluate part. Looking at the website in headless mode=false it just loads infinitely (after a click). Also if I manually try "document.querySelector" on that page that is stuck in loading, it works for me.

The code is the following:

    console.log("Test");
    let value = await page.evaluate((sel) => {
        let element = document.querySelector(sel);
        return element? element.innerHTML: null;
    },selector);
    console.log("Test2");

What can I do to prevent it from running that long (I would try to setup some kind of timeout system for this case)?
Or how could I track the time while the code is in this part? The code immediately after this part never runs (only after hours) probably because of the await.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

无法言说的痛 2025-02-19 09:58:58

您应该等待Page.Waitforselector(SEL,{Waituntil:timeout})首先评估

You should await page.waitForSelector(sel, { waitUntil:timeout }) first then evaluate

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文