下面的代码从第三方服务器检索html并通过管道将其传送到客户端应用程序,该应用程序在安装了请求包的node.js、express.js上运行。
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管道获取内容(或者如何在管道中编写)来删除标签和所有子节点。
发布于 2020-12-11 00:43:23
在我看来,就您的设置而言,这种处理属于服务器端。
假设你在一个变量html
中有一个超文本标记语言字符串,然后你可以删除center
标签,如下所示:
const newHtml = $('<div/>').append(html).find('center').remove().end().html();
演示
const $remote = $('<div/>').append($('#remote-content'));
console.log( $remote.html() );
const $nocenter = $remote.find('center').remove().end();
console.log( $nocenter.html() );
<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
const $ = cheerio.load(html); //html variable has the remote html string
$('center').remove();
const newHtml = $.html();
https://stackoverflow.com/questions/65236775
复制相似问题