ruby nokogiri - 在嵌套div中抓取内容

时间:2014-12-14 19:50:51

标签: css ruby nokogiri

这是评论

<div id="md container">
    <ul class="unstyled inline span12"  style="margin-top: … /ul>
        <div id="md_func"
            <ul class"unstyled inline span4 ng-scope"ng-init… 
                <li ng-repeat="event in events" class"span12 ng-scope …
                    <div class="func" ng-class"…
                        <span class="name" event-nowatch="name">XYZ_want_to_get</span>
                        <span class="pull-left address" event-nowatch="address">1234 maries ct</span> 
                   </div>
               </li>
           </ul>   
           </u> … </ul>
           </u> … </ul>
       </div>
</div>

如何在嵌套div中获得“XYZ_want_to_get”?我尝试过xpath和css组合,但实际上永远不会得到值“XYZ_want_to_get”。我使用了检查器来公开路径 - 当使用时输出节点源或什么都没有。

@contents = Nokogiri::HTML(open(url))

puts "css"
puts @contents.at_css('#markdown_functions > ul:nth-child(1) > li:nth-child(1) > div.function > span.name').text

puts "xpath"
puts @contents.at_xpath('//*[@id="markdown_functions"]/ul[1]/li/div[1]/span[1]/text()')

puts "search"
puts @contents.search('//CORE').first.text
提前谢谢 dp33

1 个答案:

答案 0 :(得分:0)

您可以通过以下方式获得“XYZ_want_to_get”:

@contents = Nokogiri::HTML(open(url))
@contents.css('span.name').text

证明:

2.1.4 :015 > page.css('span.name').text
=> "XYZ_want_to_get"