如何使用perl中的WWW::Scripter模块设置referrer

How to set referrer using WWW::Scripter module in perl?

本文关键字:Scripter 模块 设置 referrer WWW 何使用 perl 中的      更新时间:2023-09-26

我的爬网程序的简单代码是:

      #!/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之前,我需要设置referr标头。或者,我还需要设置其他标头,如cookie等。我在文档中看不到如何做到这一点。必须有一种方法,如何设置标头。怎样

WWW::Scripter是WWW::Mechanical的一个子类,因此您也应该能够使用该类的方法。它应该是这样的:

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');

这是WWW::Mechanize的子类,所以:

$w->add_header(Referer => "http://...");