【问题标题】:Load webpage's content 10s after the main load主加载后 10 秒加载网页内容
【发布时间】:2020-05-08 16:44:15
【问题描述】:

您知道如何在加载 10 秒后检索页面内容吗?有一个页面正在加载,然后在加载10秒后,它收到信息,突然我等待加载事件时没有收到任何信息。

这是一个需要一定时间才能接收其内容的页面示例:https://utip.io/video-overlay/433081f034fda0e01e63ca2d8ab71cca?type=tag&tag=goal-amount

在 NodeJs 中,我尝试使用带有 http.get() 的“http”模块,但我无法恢复任何内容,因为信息在加载后 10 秒到达 ....

function getInfo(link) {
  http.get(link), function(res) {
    res.on('data', function(data) {
        console.log(data)
        //Return nothing because there is nothing on the page
    })
    res.on('error', function(err) {
        console.log(err)
    })
  }
}
getInfo("https://utip.io/video-overlay/433081f034fda0e01e63ca2d8ab71cca?type=tag&tag=goal-amount")

【问题讨论】:

  • 请在您遇到问题的地方发布代码 sn-p。
  • 我认为您应该使用https 而不是http。如果你想延迟获取数据,你可以使用setTimeout()
  • 如果我使用https 我得到一个错误TypeError [ERR_INVALID_PROTOCOL]: Protocol "https:" not supported. Expected "http:" setTimeOut() 在这种情况下不起作用
  • 你在导入https吗?即const https = require("https"); and then you should use https` 而不是http 编辑:这里是replit link。试试看告诉我
  • 不工作,我认为你不明白这个问题。我找到了解决方案:

标签: javascript html node.js


【解决方案1】:

好的,我找到了解决方案,我们需要使用puppeteer 模块。 npm i puppeteer

(async () => {
     let webpage;
     let browser = await puppeteer.launch();
     let page = await browser.newPage();
     await page.goto(link);
     await timeout(15000)
     await page.content().then((res) => console.log(res))
     await browser.close();
})
function timeout(ms) {
     return new Promise(resolve => setTimeout(resolve, ms));
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-05-25
    • 1970-01-01
    • 2013-12-30
    • 1970-01-01
    • 1970-01-01
    • 2012-10-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多