如何使用 LWP 设置 User-Agent? [英] How to set User-Agent with LWP?

查看:48
本文介绍了如何使用 LWP 设置 User-Agent?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有 Perl &LWP 书,但如何设置用户代理字符串?

I have the Perl & LWP book, but how do I set the user-agent string?

这就是我所拥有的:

use LWP::UserAgent;
use LWP::Simple; # Used to download files

my $u = URI->new($url);
my $response_u = LWP::UserAgent->new->get($u);
die "Error: ", $response_u->status_line unless $response_u->is_success;

如果我想像这里一样使用 LWP::UserAgent 有什么建议吗?

Any suggestions, if I want to use LWP::UserAgent like I do here?

推荐答案

来自 LWP 说明书:

  use LWP::UserAgent;
  $ua = new LWP::UserAgent;
  $ua->agent("$0/0.1 " . $ua->agent);
  # $ua->agent("Mozilla/8.0") # pretend we are very capable browser

  $req = new HTTP::Request 'GET' => 'http://www.sn.no/libwww-perl';
  $req->header('Accept' => 'text/html');

  # send request
  $res = $ua->request($req);

这篇关于如何使用 LWP 设置 User-Agent?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆