0

我的爬虫的简单代码是:

      #!/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等。我在文档中没有看到如何做到这一点。必须有一种方法,如何设置标题。如何?

4

2 回答 2

5

WWW::ScripterWWW::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');
于 2012-03-05T15:21:24.467 回答
1

这是WWW::Mechanizeso的子类:

$w->add_header(Referer => "http://...");
于 2012-03-05T15:18:11.410 回答