I am scraping a site and I found this
我正在刮一个网站,我发现了这个。
<table>
<tr>
<td>
<b>Status:</b>ACTIVE;
<b>Type:</b>CN - CONSTRUCTION
<b>Added:</b>02/24/2012
</td>
</tr>
</table>
How do I get status
, type
, and added
individually?
如何获得状态、类型和单独添加?
I know I will get downvotes because I am not posting any TRIED CODE... but I cant even seem to think what to try!
我知道我会得到选票,因为我没有发布任何尝试过的代码……但我甚至连想都不敢想!
This website has POOR HTML structure and I cant seem to find any way.
这个网站的HTML结构很糟糕,我似乎找不到任何方法。
2 个解决方案
#1
2
- Use
jQueryElement.text()
to grab all the text. - 使用jQueryElement.text()来获取所有文本。
- Use
String#spplit
to split the string - 使用字符串#spplit分割字符串。
var text = $('#content').text();
var split = text.trim().split('\n');
split.forEach(function(el) {
var splitAgain = el.split(':');
console.log("Key: " + splitAgain[0].trim() + " Value: " + splitAgain[1].trim());
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<table>
<tr>
<td id="content">
<b>Status:</b>ACTIVE;
<b>Type:</b>CN - CONSTRUCTION
<b>Added:</b>02/24/2012
</td>
</tr>
</table>
#2
1
Javascript nextSibling
property get next text sibling of element. You can select b
elements in td
and get next text of it.
Javascript nextSibling属性获得元素的下一个文本。你可以在td中选择b元素,然后得到它的下一个文本。
$("td > b").each(function(){
console.log(this.innerText +" = "+ this.nextSibling.nodeValue.trim());
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table>
<tr>
<td>
<b>Status:</b>ACTIVE;
<b>Type:</b>CN - CONSTRUCTION
<b>Added:</b>02/24/2012
</td>
</tr>
</table>
#1
2
- Use
jQueryElement.text()
to grab all the text. - 使用jQueryElement.text()来获取所有文本。
- Use
String#spplit
to split the string - 使用字符串#spplit分割字符串。
var text = $('#content').text();
var split = text.trim().split('\n');
split.forEach(function(el) {
var splitAgain = el.split(':');
console.log("Key: " + splitAgain[0].trim() + " Value: " + splitAgain[1].trim());
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<table>
<tr>
<td id="content">
<b>Status:</b>ACTIVE;
<b>Type:</b>CN - CONSTRUCTION
<b>Added:</b>02/24/2012
</td>
</tr>
</table>
#2
1
Javascript nextSibling
property get next text sibling of element. You can select b
elements in td
and get next text of it.
Javascript nextSibling属性获得元素的下一个文本。你可以在td中选择b元素,然后得到它的下一个文本。
$("td > b").each(function(){
console.log(this.innerText +" = "+ this.nextSibling.nodeValue.trim());
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table>
<tr>
<td>
<b>Status:</b>ACTIVE;
<b>Type:</b>CN - CONSTRUCTION
<b>Added:</b>02/24/2012
</td>
</tr>
</table>