我正在尝试使用Perl的WWW::Mechanize登录到我的银行并提取交易信息。通过浏览器登录我的银行(富国银行)后,它会简短地显示一个临时网页,上面写着“请稍候,我们正在核实您的身份”。几秒钟后,它进入银行的网页,在那里我可以获得我的银行数据。唯一的区别是,该URL包含多个附加到临时页面的URL的"GET“参数,该临时页面只有一个sessionID参数。
我能够成功地从登录页面获得WWW::Mechanize登录,但它在临时页面上卡住了。有一个<meta http-equiv="Refresh"...标记,所以我尝试了$mech->follow_meta_redirect,但它也不能让我通过那个临时页面。
任何帮助克服这个问题的人都将不胜感激。提前谢谢。
下面是让我停留在临时页面的基本代码:
#!/usr/bin/perl -w
use strict;
use WWW::Mechanize;
my $mech = WWW::Mechanize->new();
$mech->agent_alias( 'Linux Mozilla' );
$mech->get( "https://www.wellsfargo.com" );
$mech->submit_form(
form_number => 2,
fields => {
userid => "$userid",
password => "$password"
},
button => "btnSignon"
);发布于 2010-12-15 12:38:40
对不起,我已经好几年没有编写Perl了。然而,由于这个问题还没有“复制粘贴”的答案,下面是如何在Ruby中抓取Wells Fargo的方法:
require 'rubygems'
require 'mechanize'
username = 'your_username'
password = 'your_password'
agent = Mechanize.new
agent.user_agent_alias = 'Windows IE 6'
# get first page
page = agent.get('https://online.wellsfargo.com/signon/')
# find and fill form
form = page.form_with(:name => 'Signon')
form['userid'] = username
form['password'] = password
page = agent.submit form
# find the refresh url
page.body.match /content="1;URL=(.*?)"/
nexturl = $1
# wait a little while and then get the next page
sleep 3
page = agent.get nexturl
# If you have multiple accounts, you can use this. If you just have a single account, you can remove this block
companies = [['Account1', '123456789'],
['Account2', '123456789']]
companies.each do |name, id|
form = page.form_with(:name => 'ChangeViewFormBean')
form['viewKey'] = id
page = agent.submit form
available_balance = page.search("#cashTotalAvailBalance").text.strip
puts "#{name}: #{available_balance}"
sleep 2
end引用的作品:有一个人写了这个脚本的一个版本,将其发布到他的代码目录中,然后将整个内容转发到他的博客。他的姓是杨布拉德或类似的。我在互联网存档/很久以前的机器中找到了源代码,并对其进行了修改,使其与您上面看到的一样。所以,感谢杨布拉德先生或类似的人,无论你在哪里-也感谢你教我元抓取技巧!
发布于 2010-04-30 03:56:20
您需要对中间页面上发生的事情进行反向工程。例如,它是否使用Javascript来设置一些cookie?Mech不会在页面上解析或执行Javascript,所以它可能试图遵循元刷新,但缺少一些关于最终请求需要发生什么的关键信息。
尝试使用像Firebug这样的工具来观察当浏览器遵循元刷新时发送的请求。检查发送来请求最终页面的所有请求头,包括cookies。然后使用Mech来复制它。
发布于 2010-04-30 04:00:40
如果您知道下一页的位置,可以尝试在附加额外的get参数后使用以下命令获取它
$mech->add_header($name => $value);https://stackoverflow.com/questions/2740235
复制相似问题