【问题标题】:Extracting data from a web page and inserting into html code of another webpage从网页中提取数据并插入到另一个网页的 html 代码中
【发布时间】:2020-05-14 00:06:38
【问题描述】:

我有多个网页,在 HTML 代码中只包含一个数字,没有别的。这些是从我制作的 IOT 设备上更新的。我还有一个主数据网页,旨在在一页上显示所有数据。但是,我无法弄清楚如何在主数据网页的 HTML 代码中提取数据网页的编号。

我发现的唯一解决方法是使用 iframe,但这似乎很笨拙,而且不是“赏心悦目”。有没有办法使用 HTML 或 javascript 来做到这一点?我对网络开发非常陌生。唯一的限制是我无法更改从 IoT 设备上传数据的方式,因此必须从仅包含数字的单个数据网页中提取数据。

提前谢谢:)

【问题讨论】:

标签: javascript html


【解决方案1】:

找到 iFrame 的 innerHTML 的 Javascript 选择器后,您可以从 iframe 中获取值以在父级或窗口中使用:

//Getting a value from the first iframe
//Assumes the page in the iframe only contains a single plaintext value
myValue = document.querySelectorAll('iframe')[0].contentWindow.document.body.innerText;

为了使其更具吸引力,您可以轻松隐藏 iFrame:

//Hiding the embedded iframes
for (var i=0; i<document.querySelectorAll('iframe').length; i++){
    document.querySelectorAll('iframe')[i].style.visibility = "hidden";
};

【讨论】:

    【解决方案2】:

    你需要做的是

    1. 获取 html 页面
    2. 解析每一页的数据
    3. 将其呈现到您的当前页面

    这不是一项简单的任务,主要是因为解析和处理可能需要随机时间才能完成的多个提取。和浏览器安全限制,以防文件位于不同的域中。 如果单个文件是 .json 文件而不是 html 文件会更好。

    这是一个带有简单示例的代码框:https://codesandbox.io/s/sleepy-borg-kuye6?file=/src/index.js

    这是一个教程: https://gomakethings.com/getting-html-with-fetch-in-vanilla-js/

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-12-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多