2011-07-21 15 views
7

Ruby gem Mechanizeを使用してAmazonにログインしようとしています。どんな種類のエラーメッセージも表示せずに、常にサインインページに戻ってきます。私はこれがMechanizeのバグか、Amazonがこの種のアクセスをブロックしているかどうか疑問に思います。私はあなたがテストすることができます以下のコードを持っています。RubyでAmazonにログインできませんMechanize

@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 = "[email protected]" 

radiobutton = form.radiobutton_with(:name => 'create', :value => '0') 
radiobutton.check 

button = form.button_with(:id => "signInSubmit") 

@page = form.submit button 

ありがとうございました。

答えて

8

mechanize documentationは、いくつかのクールな例があり

#!/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 = "[email protected]" 
    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 

、これを試してみてください。このcheat sheetは、クイックリファレンスにも便利です。

+0

私は似たようなことをしようとしていますが、あまり成功していません。上記のコードは、ログインページにリダイレクトされます。何か案は? – fffanatics

+0

私も知りたいです –

+0

@fffanatics、@Zackファイルをページに印刷するコードを更新しました。 'form.email'と' form.password'の値をあなたのAmazonユーザの資格に合わせて変更してください。それはまだ私のために働いています。 –

関連する問題