使用Perl从Google insight for search下载.csv文件时出现问题
我正在尝试使用perl从googleinsights for search下载.csv文件。但我遇到两个问题:使用Perl从Google insight for search下载.csv文件时出现问题,perl,Perl,我正在尝试使用perl从googleinsights for search下载.csv文件。但我遇到两个问题: 似乎下载URL是重定向的,所以我不能用LWP模块下载它。 网址是 "http://www.google.com/insights/search/overviewReport?q=dizzy&date=1%2F2012%205m&cmpt=date&content=1&export=1". 你可以试试,可能应该先登录 似乎我必须在下载之前存储会话。如果不这样做,我会收到类似“达到配额限制
#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";
}
我强烈建议您使用WWW::Mechanize实现web自动化(这是高级LWP::UserAgent):
谢谢你,伙计!我将看到perl文档。还有一个问题。您将$mech->content()保存到文件中,但为什么我需要的数据在$mech->content()中?我的意思是,url不是一个可以直接给你.csv文件的下载链接,它是一个重定向链接吗?它向您发送了什么?您只需给
$mech
一个链接,它就会为您处理所有重定向。
#!/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;