perl - 如何让 WWW-Mechanize 登录富国银行网站?

标签 perl www-mechanize

我正在尝试使用 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"
);

最佳答案

抱歉,我已经好几年没有编写 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

引用的作品:有一个人编写了该脚本的一个版本,将其发布到他的代码目录,然后将整个内容转发到他的博客。他的姓是 Youngblood 或类似的名字。我在互联网文件/回程机器中找到了源代码,并将其修改为您在上面看到的内容。所以,感谢 Youngblood 先生或类似的人,无论您身在何处 - 并且感谢您教我元抓取技巧!

关于perl - 如何让 WWW-Mechanize 登录富国银行网站?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2740235/

相关文章:

perl WWW::Mechanize,链接重定向问题

perl - 我怎样才能使 WWW :Mechanize to not fetch pages twice?

Perl:修复 Moose 属性和类型强制转换问题

perl - 如何编写一个程序来读取文本文件并替换某些单词,然后以不同的名称输出该文本文件

mysql - 为什么我会收到此 perl 代码的错误 500?

perl - 无法用perl解析html标签

perl - WWW:机械化表格选择

c - Perl XS : Memory management

Perl -e 测试奇怪吗?

perl - 在 WWW::Mechanize 中设置基本身份验证凭据