获取两个标签之间的HTML
Get HTML between two tags
试图从内部论坛获取一些HTML资源。为了独立,我们使用nodejs, express和类似的东西。
当我直接打开页面时,我得到以下html返回:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="content-type" content="text/html; charset=us-ascii" />
<meta name="description" content="myForum" />
<meta name="viewport" content="width=320; user-scalable=no" />
<title>myForum</title>
</head>
<body>
<table>
<tr>
<td align="left" valign="top" width="100%">
<center>
<h1><img class="banner" src=
"./img/myForum.jpg" width="730"
height="117" border="0" alt="myForum" /></h1>
</center>
<hr />
<center>
[ <a href="answerswer.php?id=975710">Antworten</a> ] [
<a href="index.php">Forum</a> ] [ <a href=
"newEntries.php">Neue Beiträge</a> ]
</center>
<hr />
<h1>sCHween</h1>geschrieben von <font color=
"#FFFFFF">User1</font> am 18.06.2014 um 21:26:15
<hr />
This is my text! It could contain images and links!
<img src="http://images.google.ch/intl/en_ALL/images/srpr/logo11w.png" /><br />
<a href="http://www.google.com/">Google</a>
<br />
<hr />
<b>Antworten:</b><br />
<a href="thread.php?id=9752">Re:
sCHween</a> - <b><font color=
"#FFFFFF">User2</font></b> - 18.06.2014 22:56:27<br />
<a href="showentry.php?id=9756">Re:
sCHween</a> - <b><font color=
"#FFFFFF">User2</font></b> - 18.06.2014 23:14:44<br />
<a href="showentry.php?id=9753">Re:
sCHween</a> - <b><font color=
"#FFFFFF">User1</font></b> - 18.06.2014 23:02:21<br />
<a href="showentry.php?id=975713">Re:
sCHween</a> - <b><font color=
"#FFFFFF">User1</font></b> - 18.06.2014 21:46:13<br />
<a href="showentry.php?id=9720">Re:
sCHween</a> - <b><font color=
"#FFFFFF">User3</font></b> - 18.06.2014 22:22:25<br />
<a href="showentry.php?id=9755">Re:
sCHween</a> - <b><font color=
"#FFFFFF">User4</font></b> - 18.06.2014 21:52:51<br />
<hr />
<span>
<a href="answerswer.php?id=975">Antworten</a><br />
<a href="recent.php">Neue Beiträge</a><br />
</span>
<hr />
</td>
</tr>
</table>
</body>
</html>
我们想要得到的是两个hr标签之间内容的html源:
This is my text! It could contain images and links!
<img src="http://images.google.ch/intl/en_ALL/images/srpr/logo11w.png" /><br />
<a href="http://www.google.com/">Google</a>
是否有一种简单的方法来获取两个hr标签之间的源,或者提取此内容的最干净和最简单的方法是什么?
不确定这是否是你想要的:
Jquery:var AllContent = $("td").contents();
var hrCount = 0;
var addContent = false;
var result="";
AllContent.each(function(){
if ($(this).prop('tagName') == "HR"){
hrCount++;
if (hrCount ==3){
addContent = true;
}
if (hrCount ==4){
addContent = false;
}
}else{
if(addContent){
if (typeof $(this).html() != "undefined"){
result+=$(this)[0].outerHTML;
}else{
result+=$(this).text();
}
}
}
});
alert(result);
jsdom是在node中进行DOM解析的一个很好的工具。由于您希望将文本节点和常规元素都转换为字符串,因此我们必须对两者进行区分:
var jsdom = require("jsdom");
jsdom.env(
'http://example.com',
['http://code.jquery.com/jquery.js'],
function (errors, window) {
var $hr = window.$('hr'),
node = $hr.get(2).nextSibling,
endNode = $hr.get(3),
html = '';
while (node && node !== endNode) {
if (node.nodeType === 3) {
html += node.textContent;
} else {
html += node.outerHTML;
}
node = node.nextSibling;
}
}
);
现在html
的值如下:
This is my text! It could contain images and links!
<img src="http://images.google.ch/intl/en_ALL/images/srpr/logo11w.png"><br>
<a href="http://www.google.com/">Google</a>
<br>
相关文章:
- JavaScript中的RegEx:两个标签之间的内容
- 有没有一种方法可以从两个标签之间提取文本,并以我选择的格式输出
- 谷歌应用程序在两个标签之间编写目标文本
- 使用 JavaScript 删除标签之间的每个空格
- 在<选项>标签之间切换时保存输入文本
- 标签之间的内容转换为标题JS
- 删除 p标签之间
- 阅读标签之间的文本
- 在谷歌浏览器中,标签之间最快的通信是什么
- 崇高文本 直接在标签之间上下移动光标
- 结束标签到开始标签之间的Javascript reg exp
- 如何在离子标签之间添加常见内容
- 在 php 标签之间运行 javascript
- 隐藏<标签>abc标签>之间的文本 - 删除“ ABC”
- 咏叹调标签和咏叹调标签之间的区别
- 如何删除两个标签之间的文本
- React路由器中Link和Anchor标签之间的不同行为
- 隐藏2个h2标签之间的所有内容
- 什么's在我的脚本标签之间有Razor
- 我想使用jquery在标签之间附加锚文本