使用机械化时,使用正则表达式查找具有某个锚点的链接

时间:2012-05-09 07:39:50

标签: ruby regex mechanize

我正在尝试使用Mechanize link_with(:href =>'anchor here')来查找与href中某个字符串有链接的页面。例如,我想要它,以便我可以将所有具有链接的网站吐出到文本文件中,其中锚点包含“index.php?user”

我该怎么做?

3 个答案:

答案 0 :(得分:5)

全部谢谢你的答案,我最终选择了page.link_with(:href => /(.*)?user $ /)

答案 1 :(得分:1)

urls = ['http://www.google.com/','http://www.foo.com/','http://www.bar.com/']

File.open('output.txt', 'w') do |out|
  urls.each do |url|
    out << url if agent.get(url).link_with(:href => /index.php\?user/)
  end
end

答案 2 :(得分:0)

我建议你研究一下XPath选择器:

jQuery Xpath selector to select an element which id contains 'sometext'

有关如何在机械化中使用XPath的示例,请访问:

extract single string from HTML using Ruby/Mechanize (and Nokogiri)