我尝试使用Ruby Mechanize gem登录amazon partnernet,例如https://partnernet.amazon.de/:
宝石文件:
# https://github.com/sparklemotion/mechanize
gem 'mechanize'
下面的代码是一个rake任务。它在过去工作,我认为亚马逊改变了页面html所以这段代码不再工作,例如,通过改变表单name=“sign_in”的submit按钮为image type=input。
desc "Cron Task for Email Notifications"
task :email_amazon_stats => :environment do
puts "Start: Fetch and send Amazon Sales from yesterday (#{Time.now})"
# login to Amazon Partnernet
a = Mechanize.new
a.user_agent_alias = 'Mac Safari'
a.follow_meta_refresh = true
a.redirect_ok = true
a.get('https://partnernet.amazon.de/') do |page|
# Submit the login form
page.form_with(:name => 'sign_in') do |f|
username_field = f.field_with(:id => "username")
username_field.value = "email@example.com"
password_field = f.field_with(:id => "password")
password_field.value = "somepassword"
end.submit
start_date = Time.now - 1.day
end_date = Time.now
my_page2 = a.get("https://partnernet.amazon.de/gp/associates/network/reports/report.html?ie=UTF8&deviceType=all&endDay=#{(end_date.strftime('%d').to_i).to_s}&endMonth=#{((end_date.strftime('%m').to_i)-1).to_s}&endYear=#{end_date.strftime('%Y').to_i.to_s}&periodType=exact&preSelectedPeriod=monthToDate&program=all&reportType=earningsReport&startDay=#{start_date.strftime('%d').to_i.to_s}&startMonth=#{((start_date.strftime('%m').to_i)-1).to_s}&startYear=#{start_date.strftime('%Y').to_s}")
form = my_page2.form_with(:name => 'htmlReport')
button = form.button_with(:name => 'submit.download_XML')
xml = a.submit(form, button)
# ASIN="3423347570"
# Binding="paperback"
# Category="14"
# Date="December 01, 2015"
# DeviceType="BROWSER"
# EDate="1448928000"
# Earnings="0,65"
# LinkType="asn"
# Price="9,25"
# Qty="1"
# Rate="7,03"
# Revenue="9,25"
# Seller="Amazon.de"
# Tag="yx-21"
# Title="Kopf schlägt Kapital: Die ganz andere Art, ein Unternehmen zu gründen Von der Lust, ein Entrepreneur zu sein (dtv Sachbuch)"/>
doc = Nokogiri::XML(xml.body)
@sales = []
doc.xpath("//Item").each do |item|
@sales << {
:sale_itemasin => item['ASIN'],
:sale_itemname => item['Title'].truncate(80),
:sale_date => Time.at(item['EDate'].to_i).strftime("%Y-%m-%d %H:%M:%S").to_s,
:sale_amount => '%.2f' % item['Revenue'].gsub(',','.').to_f,
:sale_commission => '%.2f' % item['Earnings'].gsub(',','.').to_f
}
end
earnings = 0
@sales.each do |s|
earnings += s[:sale_commission].to_f
end
@total_commission = '%.2f' % earnings
end
ReportsMailer.daily_dashboard(@total_commission,@sales).deliver
puts "Done: Fetch and send Amazon Sales from yesterday (#{Time.now})"
end
有人能帮我一下吗?
我寻找类似的问题如何重组提交,但到目前为止没有任何工作。登录没有发生。(是的,PWD是正确的:-))
类似的问题,但并没有解决上面的问题:Cannot Login to Amazon with Ruby Mechanize
1条答案
按热度按时间esbemjvw1#
所以..我调试了代码,这个版本现在工作如预期:
正如你所看到的,这是非常丑陋的,因为我试图直接进入deeplink,它会把我重定向到登录页面。在那里我登录并再次尝试进入 Jmeter 板。这次它工作了。为什么丑陋?因为如果我试图直接进入登录页面的代码不工作,我不知何故需要这个重定向。你知道为什么吗?了解这个会很有趣...