【问题标题】:Problems when downloading .csv files from Google insight for search using Perl从 Google 洞察下载 .csv 文件以使用 Perl 进行搜索时出现问题
【发布时间】:2012-08-02 23:12:42
【问题描述】:

我正在尝试使用 perlgoogle 搜索洞察 下载 .csv 文件。但是我遇到了两个问题:

  1. 下载地址好像是重定向的,所以不能用LWP模块下载。 网址是 “http://www.google.com/insights/search/overviewReport?q=dizzy&date=1%2F2012%205m&cmpt=date&content=1&export=1”。你可以试试,应该先登录。

  2. 看来我必须在下载之前存储会话。如果不这样做,我会收到警告 - 例如“达到配额限制”。

如何使用 PERL 自动下载此 .csv 文件?感谢您的帮助。

这是我的代码:

#create userAgent object
my $ua = LWP::UserAgent->new;
$ua->agent("MyApp/0.1 ");

#create a request
my $req = HTTP::Request->new(GET => 'http://www.google.com/insights/search/overviewReport?q=dizzy&date=1%2F2012%205m&cmpt=date&content=1&export=1');

my $res = $ua->request($req);

#check the outcome of the response
if($res->is_success) {
    print $res->content;
}
else {
    print $res->status_line, "\n";
}    

【问题讨论】:

    标签: perl


    【解决方案1】:

    我强烈建议您使用 WWW::Mechanize 进行网络自动化(这是高级 LWP::UserAgent):

    #!/usr/bin/perl
    
    use strict;
    use warnings;
    use WWW::Mechanize;
    
    my $mech = WWW::Mechanize->new();
    $mech->agent_alias("Windows IE 6");
    
    $mech->get("https://accounts.google.com/serviceLogin");
    $mech->submit_form(
    
        form_id => "gaia_loginform",
        fields => {
    
            Email => 'gangabass@gmail.com',
            Passwd => 'password',
        },
        button => "signIn",
    );
    
    $mech->get("http://www.google.com/insights/search/overviewReport?q=dizzy&date=1%2F2012%205m&cmpt=date&content=1&export=1");
    open my $fh, ">:encoding(utf8)", "report.csv" or die $!;
    print {$fh} $mech->content();
    close $fh;
    

    【讨论】:

    • 非常感谢老兄!我会看到 perl 文档。
    • 还有一个问题。您将 $mech->content() 保存到文件中,但为什么我需要的数据在 $mech->content() 中?我的意思是,该网址不是可以直接为您提供 .csv 文件的下载链接,它是重定向的吗?它给你带来了什么?
    • 您只需给$mech 一个链接,它就会为您处理所有重定向。
    猜你喜欢
    • 1970-01-01
    • 2022-09-14
    • 1970-01-01
    • 1970-01-01
    • 2020-01-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-24
    相关资源
    最近更新 更多