Xpath和Nokogiri:为什么我不能得到这个属性?

时间:2012-07-22 18:06:59

标签: ruby-on-rails xpath nokogiri

我正在尝试解析看起来像的HTML:

<p class="row">
            <span class="itemdate"> Jul 22</span>
            <span class="itemsep"> - </span> 
            <a href="http://newyork.craigslist.org/brk/abo/3102470187.html">$2000 / 3br - Three bedroom apartment/new renovtion</a>
            <span class="itemsep"> - </span>
            <span class="itempn"><font size="-1"> (Bushwick-L Train-Close to Williamsburg)</font></span>
            <span class="itempx"> <span class="p"> pic</span></span>
            <span class="itemcg" title="abo"> <small class="gc"><a href="/abo/">apts by owner</a></small></span>
        </p>

当我执行此呼叫时:

page.xpath("//p[contains(@class, 'row')]/a").first

我得到的结果如下:

#<Nokogiri::XML::Element:0x3feea2631444 name="a" 
attributes=[#<Nokogiri::XML::Attr:0x3feea2631390 name="href"
value="http://newyork.craigslist.org/brk/abo/3102470187.html">] 
children=[#<Nokogiri::XML::Text:0x3feea2630ad0 "$2000 / 3br - Three bedroom">]>

因此,通过查看xpath文档,我应该能够通过执行以下操作返回URL:

page.xpath("//p[contains(@class, 'row')]/a@href").first 

但是我收到了这个错误:

Nokogiri::XML::XPath::SyntaxError Exception: Invalid expression: 
//p[contains(@class, 'row')]/a@href

我知道如何从Nokogiri对象获取URL,但我宁愿xpath搜索只返回一个URL数组。

1 个答案:

答案 0 :(得分:2)

你错过了斜线,需要.../a/@href