首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >node.js从外部服务器检索html删除检索到的html中<center>标记下的所有内容并通过管道连接到客户端

node.js从外部服务器检索html删除检索到的html中<center>标记下的所有内容并通过管道连接到客户端
EN

Stack Overflow用户
提问于 2020-12-10 22:48:46
回答 1查看 27关注 0票数 1

下面的代码从第三方服务器检索html并通过管道将其传送到客户端应用程序,该应用程序在安装了请求包的node.js、express.js上运行。

代码语言:javascript
运行
复制
app.get('/Redirect', function(req, res) {
        var url = 'https://www.example.com/sample.html'
        res.contentType("application/html");
        req.pipe(request(url)).pipe(res);
}); 

不要求调试上面的内容。它工作,当在node.js上运行时,它检索html并将html通过管道传送到浏览器上的客户端应用程序。但是需要在管道中使用jquery的帮助。客户端应用程序只需调用www.clientwebsiteexample.com/request,代码将返回sample.html,以便使用js进行进一步处理。

在服务器端要解决的问题是:在将res输出到客户端浏览器之前,在检索到的html中的<center> </center>标记下剥离所有内容,包括所有子内容和<center> </center>标记。该页面有多个<center> </center>标记。其余的html标签应该保持不变。

在一个普通的网页上,可以尝试像$('<center>').html(url).children().remove().end().html();这样的东西,但是,不确定如何在管道内或在req.pipe内做到这一点。

Jquery正在Node.JS上工作,但不确定如何通过jquery管道获取内容(或者如何在管道中编写)来删除标签和所有子节点。

EN

回答 1

Stack Overflow用户

发布于 2020-12-11 00:43:23

在我看来,就您的设置而言,这种处理属于服务器端。

假设你在一个变量html中有一个超文本标记语言字符串,然后你可以删除center标签,如下所示:

代码语言:javascript
运行
复制
const newHtml = $('<div/>').append(html).find('center').remove().end().html();

演示

代码语言:javascript
运行
复制
const $remote = $('<div/>').append($('#remote-content'));
console.log( $remote.html() );
const $nocenter = $remote.find('center').remove().end();
console.log( $nocenter.html() );
代码语言:javascript
运行
复制
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="remote-content">
  <h1>Sample Header</h1>
  <p>Sample paragraph</p>
  <center>
      <h2>Sample Sub-Header</h2>
      <p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</p>
      <div>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</div>
  </center>
  <em>On more line</em>
  <center>
  Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.
  </center>
</div>

笔记

在服务器端,这将是非常相似的代码,但您将使用cheerio而不是jQuery

代码语言:javascript
运行
复制
const $ = cheerio.load(html); //html variable has the remote html string
$('center').remove();
const newHtml = $.html();
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65236775

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档