- xml - AJAX/Jquery XML 解析
- 具有多重继承的 XML 模式
- .net - 枚举序列化 Json 与 XML
- XML 简单类型、简单内容、复杂类型、复杂内容
我有一个 Mechanize 功能可以让我退出网站,但在极少数情况下它会让我超时。该功能涉及转到特定页面,然后单击注销按钮。有时,当进入注销页面或单击注销按钮时,mechanize 会遇到超时,代码会崩溃。所以我做了一个小的救援,它似乎在第一段代码下面看到的那样工作。
def logmeout(agent)
page = agent.get('http://www.example.com/')
agent.click(page.link_with(:text => /Log Out/i))
end
通过救援注销:
def logmeout(agent)
begin
page = agent.get('http://www.example.com/')
agent.click(page.link_with(:text => /Log Out/i))
rescue Timeout::Error
puts "Timeout!"
retry
end
end
假设我正确理解 rescue,即使只是点击超时,它也会重新执行这两个操作,所以为了提高效率,我想知道我是否可以在这种情况下使用 proc 并向它传递一个代码块。这样的事情会有效吗:
def trythreetimes
tries = 0
begin
yield
rescue
tries += 1
puts "Trying again!"
retry if tries <= 3
end
end
def logmeout(agent)
trythreetimes {page = agent.get('http://www.example.com/')}
trythreetimes {agent.click(page.link_with(:text => /Log Out/i))}
end
请注意,在我的 trythreetimes 函数中,我将其保留为通用 rescue,因此该函数将更易于重用。
非常感谢任何人可以提供的任何帮助,我意识到这里有几个不同的问题,但它们都是我想要学习的东西!
最佳答案
我认为您最好设置 Mechanize::HTTP::Agent::read_timeout
,而不是对某些 Mechanize 请求重试超时。归因于合理的秒数,例如 2 或 5,无论如何可以防止此请求的超时错误。
那么,您的注销过程似乎只需要访问一个简单的 HTTP GET 请求。我的意思是没有要填写的表格,所以没有 HTTP POST 请求。因此,如果我是你,我会更喜欢检查页面源代码(使用 Firefox 或 Chrome 时按 Ctrl+U),以确定你的 agent.click(page.link_with(:text =>/注销/i))
它应该更快,因为这些类型的页面通常是空白的,Mechanize 不必在内存中加载完整的 html 网页。
这是我更喜欢使用的代码:
def logmeout(agent)
begin
agent.read_timeout=2 #set the agent time out
page = agent.get('http://www.example.com/logout_url.php')
agent.history.pop() #delete this request in the history
rescue Timeout::Error
puts "Timeout!"
puts "read_timeout attribute is set to #{agent.read_timeout}s" if !agent.read_timeout.nil?
#retry #retry is no more needed
end
end
但是你也可以使用你的重试函数:
def trythreetimes
tries = 0
begin
yield
rescue Exception => e
tries += 1
puts "Error: #{e.message}"
puts "Trying again!" if tries <= 3
retry if tries <= 3
puts "No more attempt!"
end
end
def logmeout(agent)
trythreetimes do
agent.read_timeout=2 #set the agent time out
page = agent.get('http://www.example.com/logout_url.php')
agent.history.pop() #delete this request in the history
end
end
希望对您有所帮助! ;-)
关于ruby - 使用 ruby mechanize 捕获超时错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7425978/
我正在使用 mechanize python 登录网站 combochat2.us用户名 mask3和密码findnext ,但它显示了“没有找到 Mechanize 模块”之类的错误 import
我在我的 rails 应用程序中使用 gem mechanize 来抓取网页数据。 我这样使用它: agent = Mechanize.new document = agent.get("http:/
我正在学习机械。我正在尝试打开一个文本文件,您点击的链接显示 文本 (.prn)我遇到的一个问题是此页面上只有 1 个表单,并且该文件不在表单中。对我来说另一个问题是此页面上有几个文本文件,但它们都具
def return_with_soup(url): #uses mechanize to tell the browser we aren't a bot #and to retri
我正在开发一个项目,使用 python 和 Mechanize 。我有个问题 : Mechanize 返回的页面,有不是的 URLS Mechanize ,如果用户点击它,他们将通过链接 他们自己计算
问题: 解析网站时,有些字符会导致 Mechanize 无法正确解析。 提出的解决方案 解析来自网站的响应以删除这些字符 在 Mechanize 之前尝试解析它。 或者,在 Mechanize 解析网
有一个public class method将字段添加到 Mechanize 表单 我试过了.. #login_form.field.new('auth_login','Login') #login_
我有一些看起来像这样的东西: def self.foo agent = Mechanize.new form = agent.get("link/to/form/url") form.f
我在使用 Mechanize gem 时遇到问题,如何转换 Mechanize::文件 进入 Mechanize::页面 , 这是我的一段代码: **link** = page.link_with(:
我正在尝试抓取一个只能依靠类和元素层次结构来找到正确节点的站点。但是使用 Mechanize::Page#search 返回 Nokogiri::XML::Element,我不能用它来填写和提交表单等
我正在使用 mechanize 来抓取一些网页。 我需要知道什么是 Mechanize 限制? Mechanize 不能做什么? 它可以执行网页中嵌入的javascripts吗? 我可以用它来调用 j
在 WWW::Mechanize 中使用表单方法 my @form = $mech->form_number(1); foreach my $sum_form ( @form ) {
找到以下 HTML 代码: 如何使用 Mechanize 单击没有 id 和 name 的提交按钮? 最佳答案 我已经找到了此类场景的答案,代码如下: agent = Mechanize.new
这个问题不太可能对 future 的访客有帮助;它只与一个小的地理区域、一个特定的时刻或一个非常狭窄的情况相关,通常不适用于互联网的全局受众。如需帮助使这个问题更广泛地适用,visit the hel
我一直在尝试使用以下方法从终端运行 ruby 文件: ruby file_cleanse_auto.rb 但是我从 mechanize 得到一个错误: /Library/Ruby/Site/2.0
这是我拥有的代码: agent = Mechanize.new page = agent.get 'http://google.com' page.save 'google_index.htm' 我怎
for link in br.links(url_regex="inquiry-results.jsp"): cb[link.url] = link for page_link in cb.v
我有一个登录表单的页面。登录后有一些重定向。第一个看起来像这样: #"no-cache=\"set-cookie\"", "content-length"=>"114", "set-cookie"=>
我正在尝试使用基本身份验证访问 API。它适用于 HTTParty,但不适用于 2.7.6 Mechanize。 这是我尝试过的: agent = Mechanize.new agent.log =
我正在尝试使用 Ruby 的 Mechanize gem 提交表单。此表单有一组名为“KeywordType”的单选按钮。各个按钮的名称类似于 rdoAny、rdoAll 和 rdoPhrase。使用
我是一名优秀的程序员,十分优秀!