【问题标题】:Ruby Mechanize gem not following meta refreshRuby Mechanize gem 不遵循元刷新
【发布时间】:2016-11-20 23:33:57
【问题描述】:

我有一个使用 Mechanize 登录 Google Payments 的 Ruby 2.2 自动化脚本。当我尝试访问 url Mechanize 在元刷新时停止。页面内容为:

<!DOCTYPE html>
<html>
<head>
    <title>Redirecting...</title>
    <script type="text/javascript" language="javascript">
        var url = 'https:\/\/accounts.google.com\/ServiceLogin?
         service\x3dbilling\x26passive\x3d1209600\x26continue\x3dhttps:\/\/payments.google.com\/
         payments\/home%23__HASH__\x26followup\x3dhttps:\/\/payments.google.com\/payments\/
         home'; var fragment = ''; if (self.document.location.hash) {fragment = self.document.
         location.hash.replace(/^#/,'');}url = url.replace(new RegExp("__HASH__", 'g'), 
         encodeURIComponent(fragment));window.location.assign(url);
    </script><noscript><meta 
http-equiv="refresh" content="0; url='https://accounts.google.com/ServiceLogin?
 service&#61;billing&amp;passive&#61;1209600&amp;continue&#61;https://payments.google.com
 /payments/home&amp;followup&#61;https://payments.google.com/payments/home'"></meta>
</noscript></head>

<body></body>
</html>

这是我进入登录屏幕的脚本部分:

@agent = Mechanize.new  
@agent.follow_meta_refresh = true  
page = @agent.get("http://payments.google.com/payments/home")  
puts page.content

最后的page.content只显示上面的html,没有跟着meta刷新。任何关于我如何遵循的建议将不胜感激。

【问题讨论】:

  • @Idempotence 我认为您的想法是正确的,但我自己 reformatted 是,因为这比校对更快。
  • 你实际上不能像这样在 url 中有换行符,它会破坏脚本。
  • @pguardiario 有人认为将返回值的原始帖子格式化会很聪明。原始网址不包含换行符。
  • 是的,我就是这么想的 :) @jpaugh 有点过分热心了。
  • @analyticsPierce, @pguardiario 我很抱歉!我使用了一个自动化工具来重新格式化它,但如果它坏了,请恢复。

标签: ruby mechanize


【解决方案1】:

假设脚本并没有像那样重新格式化:

url = page.body[/url = '(.*?)'/, 1]
page = @agent.get url

【讨论】:

  • 不幸的是,这不起作用。我认为正在检索的 url 无效或需要转换。有什么建议么?这是提取的 url 值: https:\/\/accounts.google.com\/ServiceLogin?service\x3dbilling\x26passive\x3d1209600\x26continue\x3dhttps:\/\/payments.google.com\/payments\/ home%23__HASH__\x26followup\x3dhttps:\/\/payments.google.com\/payments\/home
  • 返回的 url 有额外的反斜杠,使其无效。我用 '/' 替换了 '\/' 并且 url 工作得很好。
猜你喜欢
  • 1970-01-01
  • 2012-05-26
  • 2019-07-01
  • 1970-01-01
  • 1970-01-01
  • 2012-07-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多