Mechanize links_with没有正确过滤文本

时间:2013-12-03 00:37:36

标签: ruby mechanize mechanize-ruby

我正在尝试使用Mechanize gem单击链接列表,但显然Mechanize的links_with(criteria)未根据条件正确过滤。出于调试目的,我只打印出链接。

以下脚本打印出页面上的大多数(全部?)链接:

require 'mechanize'

agent = Mechanize.new
url = "http://www.fearlessphotographers.com/location/470/sul-do-brasil"

agent.get(url)
agent.page.links_with(:text => /[VIEW FULL PROFILE]/).each do |link|
    puts link.text
end

如果我将(:text => /[VIEW FULL PROFILE]/)更改为(:text => "VIEW FULL PROFILE"),则根本不会打印任何链接。

我无法理解我做错了什么。有什么想法吗?

1 个答案:

答案 0 :(得分:1)

括号[]在正则表达式中具有特殊含义。你需要用一个slashie /\[\]/来逃避它们。

第二个想法,这些链接中没有括号,所以请将它们排除在外。

page.links_with :text => /View Full Profile/

另请注意,文本似乎是用css获得大写()'d。