无法使用 Ruby Mechanize 登录亚马逊
Posted
技术标签:
【中文标题】无法使用 Ruby Mechanize 登录亚马逊【英文标题】:Cannot Login to Amazon with Ruby Mechanize 【发布时间】:2011-10-10 14:57:01 【问题描述】:我正在尝试使用 Ruby gem Mechanize 登录亚马逊。我总是被踢回登录页面而没有任何错误消息。我想知道这是否是 Mechanize 的错误,或者亚马逊是否阻止了这种访问。我有下面的代码,你可以通过 irb 来测试。
@mechanizer = Mechanize.new
@mechanizer.user_agent_alias = 'Mac Safari'
@page = @mechanizer.get("https://www.amazon.com/ap/signin?_encoding=UTF8&openid.assoc_handle=usflex&openid.return_to=https%3A%2F%2Fwww.amazon.com%2Fgp%2Fyourstore%3Fie%3DUTF8%26ref_%3Dpd_irl_gw&openid.mode=checkid_setup&openid.ns=http%3A%2F%2Fspecs.openid.net%2Fauth%2F2.0&openid.claimed_id=http%3A%2F%2Fspecs.openid.net%2Fauth%2F2.0%2Fidentifier_select&openid.pape.max_auth_age=0&openid.ns.pape=http%3A%2F%2Fspecs.openid.net%2Fextensions%2Fpape%2F1.0&openid.identity=http%3A%2F%2Fspecs.openid.net%2Fauth%2F2.0%2Fidentifier_select")
form = @page.form_with(:id => "ap_signin_form")
field = form.field_with(:name => "email")
field.value = "fake@email.com"
radiobutton = form.radiobutton_with(:name => 'create', :value => '0')
radiobutton.check
button = form.button_with(:id => "signInSubmit")
@page = form.submit button
感谢您的帮助。
【问题讨论】:
【参考方案1】:试试这个,
#!/usr/bin/env ruby
require "rubygems"
require "mechanize"
class AmazonCrawler
def initialize
@agent = Mechanize.new do |agent|
agent.user_agent_alias = 'Mac Safari'
agent.follow_meta_refresh = true
agent.redirect_ok = true
end
end
def login
login_url = "https://www.amazon.com/ap/signin?_encoding=UTF8&openid.assoc_handle=usflex&openid.claimed_id=http%3A%2F%2Fspecs.openid.net%2Fauth%2F2.0%2Fidentifier_select&openid.identity=http%3A%2F%2Fspecs.openid.net%2Fauth%2F2.0%2Fidentifier_select&openid.mode=checkid_setup&openid.ns=http%3A%2F%2Fspecs.openid.net%2Fauth%2F2.0&openid.ns.pape=http%3A%2F%2Fspecs.openid.net%2Fextensions%2Fpape%2F1.0&openid.pape.max_auth_age=0&openid.return_to=https%3A%2F%2Fwww.amazon.com%2Fgp%2Fyourstore%2Fhome%3Fie%3DUTF8%26ref_%3Dgno_signin"
@agent.get(login_url)
form = @agent.page.forms.first
form.email = "user@example.com"
form['ap_signin_existing_radio'] = "1"
form.password = "password"
dashboard = @agent.submit(form)
File.open('dashboard.html', 'w') |file| file << dashboard.body
end
end
AmazonCrawler.new.login
mechanize documentation 有一些很酷的例子。这个cheat sheet 也很方便快速参考。
【讨论】:
我正在尝试做类似的事情,但没有取得多大成功。上面编写的代码仍会将您重定向回登录页面。有什么想法吗? 我也想知道 @fffanatics, @Zack 我已更新代码以将页面打印到文件中。确保更改form.email
和 form.password
的值以匹配您的 Amazon 用户凭证。它仍然为我工作。
所以这绝对适用于 amazon.com。我试图登录 developer.amazon.com 而不是主网站。我无法通过 Mechanize 让它工作,但能够通过 Watir 和 Headless gems 让它工作以上是关于无法使用 Ruby Mechanize 登录亚马逊的主要内容,如果未能解决你的问题,请参考以下文章
ruby 使用CLI的Magento DB修复工具 - Ruby + Mechanize