我想用Hpricot来获取类内名称的值,我不知道。我知道它遵循模式“foo_ [几个数字] _bar”。用正则表达式搜索Hpricot
现在,我将整个包含元素作为字符串并使用正则表达式来解析标记的字符串。该解决方案有效,但它看起来真的很丑。
doc = Hpricot(open("http://scrape.example.com/search?q=#{ticker_symbol}"))
elements = doc.search("//span[@class='pr']").inner_html
string = ""
elements.each do |attr|
if(attr =~ /foo_\d+_bar/)
string = attr
end
end
# get rid of the span tags, just get the value
string.sub!(/<\/span>/, "")
string.sub!(/<span.+>/, "")
return string
看来应该有更好的方法来做到这一点。我想要做类似的事情:
elements = doc.search("//span[@class='" + /foo_\d+_bar/ + "']").inner_html
但是,这并不运行。有没有一种使用正则表达式进行搜索的方法?
这看起来像我想要的。我会试一试看看结果如何。 – AaronM 2010-01-02 22:11:01
工作完美!这正是我想要的。 – AaronM 2010-01-03 23:31:56