【问题标题】:Using a regular expression to find a link with a certain anchor when using mechanize使用mechanize时使用正则表达式查找具有某个锚点的链接
【发布时间】:2012-05-17 17:06:49
【问题描述】:

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

我该怎么办?

【问题讨论】:

    标签: ruby regex mechanize


    【解决方案1】:

    感谢大家的回答,我最终选择了 page.link_with(:href => /(.*)?user$/)

    【讨论】:

    • 谢谢你。我正在寻找一种将正则表达式输入机械化的方法。太棒了!
    【解决方案2】:
    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
    

    【讨论】:

      【解决方案3】:

      我建议您查看 XPath 选择器:

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

      可以在此处找到有关如何将 XPath 与 mechanize 一起使用的示例:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-01-16
        • 1970-01-01
        • 1970-01-01
        • 2016-07-24
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多