简体   繁体   English

从 web 页面中提取数据并插入到另一个网页的 html 代码中

[英]Extracting data from a web page and inserting into html code of another webpage

I have multiple webpages that only contain a single number in the HTML code, nothing else.我有多个网页在 HTML 代码中只包含一个数字,仅此而已。 These get updated from an IOT device I made.这些是从我制作的 IOT 设备上更新的。 I also have a main data webpage, that is meant to display all of the data on one page.我还有一个主数据网页,旨在在一页上显示所有数据。 However, I cannot figure out how to extract the number the data webpages' in the HTML code of the main data webpage.但是,我无法弄清楚如何在主数据网页的 HTML 代码中提取数据网页的编号。

The only workaround I have found is by using iframes, but this seems very clumsy, and not "pleasing for the eyes".我发现的唯一解决方法是使用 iframe,但这似乎很笨拙,而且不“赏心悦目”。 Is there any way to do this using HTML, or maybe javascript?有没有办法使用 HTML 或 javascript 来做到这一点? I am very new to web development.我对 web 开发非常陌生。 The only restriction is that I cannot change how the data is uploaded from the IoT device, so it has to be extracted from the individual data webpages that contain only numbers.唯一的限制是我无法更改从 IoT 设备上传数据的方式,因此必须从仅包含数字的单个数据网页中提取数据。

Thanks in advance:)提前致谢:)

Once you find the Javascript selector for the innerHTML of your iFrame, you can get the value out of the iframe for use within the parent or window:一旦找到 Javascript 选择器用于 iFrame 的 innerHTML,您就可以从 iframe 或 Z02FBF245B8AC74DEC706 中获取值

//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;

To make it more appealing you could easily hide the iFrame:为了使其更具吸引力,您可以轻松隐藏 iFrame:

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

What you need to do is你需要做的是

  1. Fetch the html pages获取 html 页面
  2. Parse the data from each page解析每一页的数据
  3. Render it to your current page将其渲染到当前页面

This is not trivial task, mostly because parsing and handling multiple fetches that may take random amount of time to complete.这不是一项简单的任务,主要是因为解析和处理可能需要随机时间才能完成的多个提取。 And browser security restrictions in case the files are in different domains.和浏览器安全限制,以防文件位于不同的域中。 It would be better if the individual files were.json files not html files.如果单个文件是.json 文件而不是 html 文件会更好。

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM