3

我无法专门找到这个问题,希望我没有错认为它是一个旧问题的新变体。

我希望能够在(不一致的)p.red 元素 text() 之后选择表格,其中 'p' 不包含文本“Alphabetical”但包含文本“OVERALL”..

DOM 看起来像这样:

<p class=red>Some Text</p>
  <table class="newclass">
  <tr></tr>
  <tr></tr>
</table>

<p class=red>Some Text</p>
<table class="newclass">
  <tr></tr>
  <tr></tr>
</table>

<p class=red>OVERALL</p>
<table class="newclass">
  <tr></tr>
  <tr></tr>
</table>
  • 该表在每一页都有不同的计数。

我想获得那个 p 标签的 text() ,但也想直接在它之后获得表格。同样,text() 包含“OVERALL”但不包含“ALPHABETICAL”.. 我应该构建一个数组并 .reject() 没有匹配的元素吗?目前我不确定,而且我对使用 Ruby 和 Mechanize 还很陌生,在此先感谢您的帮助!

4

2 回答 2

2

使用 Nokogiri 的 CSS 评估非常干净:

require 'nokogiri'

doc = Nokogiri::HTML(<<EOT)
<p class=red>Some Text</p>
  <table class="newclass">
  <tr></tr>
  <tr></tr>
</table>

<p class=red>Some Text</p>
<table class="newclass">
  <tr></tr>
  <tr></tr>
</table>

<p class=red>OVERALL</p>
<table class="newclass">
  <tr></tr>
  <tr></tr>
</table>
EOT

puts doc.at('p:contains("OVERALL")').to_html
# >> <p class="red">OVERALL</p>

puts doc.at('p:contains("OVERALL") ~ table').to_html
# >> <table class="newclass">
# >> <tr></tr>
# >> <tr></tr>
# >> </table>
于 2011-11-21T05:10:57.297 回答
1

p 标签:

agent.parser.xpath('//p[.="OVERALL"]')[0]

后面的表格:

agent.parser.xpath('//p[.="OVERALL"]')[0].next.next

或者:

agent.parser.xpath('//p[.="OVERALL"]/following-sibling::table[1]')[0]
于 2011-11-21T04:50:54.293 回答