具有使用javascript的网页的源代码. [英] have the source code of a webpage using javascript"

查看:89
本文介绍了具有使用javascript的网页的源代码.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想编写一个JavaScript脚本,该脚本将包含在以html编码的网页中

I would like to code a javascript script, which will be included in a webpage coded in html

<html>
<h1> My page </h1>
<script>script which gets the source code of an url </script>
</html>

此脚本将用于检索url的源代码,并在包含脚本的页面中显示此页面的代码.

This script will be used to retrieve the source code of the url and display the code of this page in the page where the script is included.

谢谢

推荐答案

在javascript中,您可以使用fetch()来检索网址

To get you started, in javascript you can use fetch() to retrieve a url

https://developer.mozilla.org/en -US/docs/Web/API/Fetch_API/Using_Fetch

示例

 fetch(url).then(response => {
    const html = response.data;
    console.log(html);
  })

如果谈到Javascript,我们当然可以在浏览器中使用Javascript,也可以在nodejs中使用

If we speak of Javascript, of course we can use Javascript in more than just browser, we can also use nodejs.

在nodejs中,我们可以使用fetch检索html,而cheerios可以解析html.

In nodejs, we can retrieve html using fetch, and cheerios to parse the html.

这篇关于具有使用javascript的网页的源代码.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆