使用两个<p>元素解析<div>,仅使用包含文本&#34; A:&#34;的<span>元素抓取</p> <p>;

时间:2018-03-11 15:38:49

标签: node.js cheerio

以下是与我合作的来源部分:

<div class="entry-content">

  <div class="at-above-post-cat-page addthis_tool" data-url="https://www.factcheck.org/2018/03/meme-falsely-claims-exposed-snopes-com/"></div>

  <p><span><span>Q:</span> </span>Did FactCheck.org expose Snopes.com as an “extremely liberal propaganda site”?</p>

  //grab this <p> element always containing <span>A:</span>: 
  <p><span><span>A:</span> </span>No. That false claim was made in a meme circulating online.<!-- AddThis Advanced Settings above via filter on get_the_excerpt --><!-- AddThis Advanced Settings below via filter on get_the_excerpt --><!-- AddThis Advanced Settings generic via filter on get_the_excerpt --><!-- AddThis Share Buttons above via filter on get_the_excerpt --><!-- AddThis Share Buttons below via filter on get_the_excerpt --></p>

  <div class="at-below-post-cat-page addthis_tool" data-url="https://www.factcheck.org/2018/03/meme-falsely-claims-exposed-snopes-com/"></div>

  <p><!-- AddThis Share Buttons generic via filter on get_the_excerpt --></p>   

</div>

我只对使用<p>抓取<span> A: </span>元素感兴趣

当我这样做时:

let rating = $(meta).children('div.entry-content').children('p').text().trim();

我从两个<p>元素中获取文本

当我这样做时:

let rating = $(meta).children('div.entry-content').children('p').next().text().trim();

或者这个:

let rating = $(meta).children('div.entry-content').children('p').eq(1).text().trim();

我得到一个空字符串

1 个答案:

答案 0 :(得分:1)

你可以通过以下方式获得第二个p孩子:

$("p:nth-of-type(2)")

或具有A:with:

p
$("p:has(span:has(span:contains(A:)))")

或者你可以像在mmm建议的那样在css之外乱搞