我的抓取工具的简单代码是:
#!/usr/bin/perl -w
use WWW::Scripter;
$w = new WWW::Scripter('agent' => 'myAgent');
$w->use_plugin('JavaScript');
### need to set a referrer header here ###
$w->get('http://website-url');
print $w->content, "\n";
我需要在执行get
之前设置引荐来源标头。或者我还需要设置其他标题,例如 Cookie 等。我在documentation中没有看到如何做到这一点。必须有一种方法,如何设置标题。怎么样?
答案 0 :(得分:5)
WWW::Scripter是WWW::Mechanize的子类,因此您也应该能够使用该类的方法。这应该是它的样子:
use strict; #ALWAYS do this
use warnings; #This too. Allows more control than -w
use WWW::Scripter;
#MODULE->new() is better than new Module() because of possible parsing ambiguity
my $w = WWW::Scripter->new('agent' => 'myAgent');
$w->add_header( Referer => 'http://somesite.com' );
$w->get('http://website-url');
答案 1 :(得分:1)
这是WWW::Mechanize
的子类,所以:
$w->add_header(Referer => "http://...");