【发布时间】:2012-05-17 17:06:49
【问题描述】:
我正在尝试使用 Mechanize link_with(:href => 'anchor here') 来查找在 href 中包含带有特定字符串的链接的页面。例如,我想要它,以便我可以将所有具有链接的站点都吐到文本文件中,其中锚点包含“index.php?user”
我该怎么办?
【问题讨论】:
我正在尝试使用 Mechanize link_with(:href => 'anchor here') 来查找在 href 中包含带有特定字符串的链接的页面。例如,我想要它,以便我可以将所有具有链接的站点都吐到文本文件中,其中锚点包含“index.php?user”
我该怎么办?
【问题讨论】:
感谢大家的回答,我最终选择了 page.link_with(:href => /(.*)?user$/)
【讨论】:
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
【讨论】:
我建议您查看 XPath 选择器:
jQuery Xpath selector to select an element which id contains 'sometext'
可以在此处找到有关如何将 XPath 与 mechanize 一起使用的示例:
extract single string from HTML using Ruby/Mechanize (and Nokogiri)
【讨论】: