jQuery解析imdb html

jQuery解析imdb html,jquery,Jquery,我有如下HTML: <div class="txt-block"> <h4 class="inline">Country:</h4> <a href="/country/us?ref_=tt_dt_dt" itemprop='url'>USA</a> </div> <div class="txt-block"> <h4 class="inline">Language:</h4>

我有如下HTML:

<div class="txt-block">
  <h4 class="inline">Country:</h4>
  <a href="/country/us?ref_=tt_dt_dt" itemprop='url'>USA</a>
</div>

<div class="txt-block">
  <h4 class="inline">Language:</h4>
  <a href="/language/en?ref_=tt_dt_dt" itemprop='url'>English</a>
</div>

<div class="txt-block">
  <h4 class="inline">Release Date:</h4> 9 January 2011 (USA)
  <span class="see-more inline">
    <a href="releaseinfo?ref_=tt_dt_dt" itemprop='url'>See more</a>&nbsp;&raquo;
  </span>
</div>
如何在这个循环中获得价值

upd:解决方案

$('.txt-block').each(function() {                                                                                                    
    var key=$(this).children('h4').text();                                                                                             
    $(this).children('h4').remove();                                                                                                   
    $(this).children('span').remove();                                                                                                 
    var value=$(this).text().trim();                                                                                                   
    console.log(key,value);
}

您可以使用
包含
选择器:

var container=$('h4:contains(“发布日期”)).parent(),
seeMore=container.find('.see more');
seeMore.detach();
console.log(container.text());
//如果需要将两者作为单独的值:
var splitValues=container.text().split(“:”);
console.log(splitValues[0]);
console.log(splitValues[1]);
//使用你的每个
$('.txt块')。每个(函数(){
var块=$(此);
if(block.find('h4:contains(“发布日期”))。长度>0){
block.find('.see more').detach()
console.log(block.text());
}
});

国家:
语言:
发布日期:2011年1月9日(美国)
&拉阔;

这将获取所选div的内容,并过滤匹配集,仅返回nodeType==3的元素,这些元素是文本节点。

简单的一个,请尝试以下操作:

$('.txt-block').each(function() {
    console.log($(this).children('h4').text());
    // strip all html
    console.log(this.textContent || this.innerText);
});

更新:要删除查看更多,请使用
$(“.see more”,$(this)).remove()

@askovpen不要像那样尝试代码,试着运行并实现你的想法。回答时只使用第二段代码。它剥离所有html并打印
h4
文本和纯文本结果是什么?XML中的值是什么?
$('.txt-block').each(function() {
    console.log($(this).children('h4').text());
    // value
    console.log($(this).first().contents().filter(function() {
        return this.nodeType == 3;
    }).text()));
});
$('.txt-block').each(function() {
    console.log($(this).children('h4').text());
    // strip all html
    console.log(this.textContent || this.innerText);
});