加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

Perl LWP::Simple 提供的方法

发布时间:2020-12-15 23:42:34 所属栏目:大数据 来源:网络整理
导读:pre name="code" class="sql"pre name="code" class="sql" get($url) The get() function will fetch the document identified by the given URL and return it. It returns "undef" if it fails. The $url argument can be either a simple string or a ref
<pre name="code" class="sql"><pre name="code" class="sql"> get($url)
          The get() function will fetch the document identified by the given URL and return it.  It returns "undef" if it fails.  The $url argument can be either a simple string 

or a reference to a URI object.

          You will not be able to examine the response code or response headers (like ’Content-Type’) when you are accessing the web using this function.  If you need that 

information you should use the full OO
          interface (see LWP::UserAgent).


get() 函数会获取给定的URL 的内容,它返回"undef"如果失败的话,$url 参数可以使一个简单的字符串 或者是一个URL 对象的引用


你不能校验 响应码 或者响应header(like ’Content-Type’),当你接收web使用这个函数的时候,如果你需要这些信息 你需要使用( LWP::UserAgent).

[root@dr-mysql01 test]# cat e1.pl 
use LWP::Simple;
$a=get('http://zjcap.cn');
print "$a is $an";


head($url)
        
Get document headers. Returns the following 5 values if successful: ($content_type,$document_length,$modified_time,$expires,$server)

          
Returns an empty list if it fails.  In scalar context returns TRUE if successful.



得到document 的headers.,返回 5个值如果称为的话  ($content_type,$server)

取网页返回header 响应header,响应头信息

如果失败返回一个空的列表
[root@dr-mysql01 test]# cat e2.pl 
 use LWP::Simple;
        @content = head("http://www.zjcap.cn/");
        die "Couldn't get it!" unless defined @content;
        print "@content is @contentn";
[root@dr-mysql01 test]# perl e2.pl 
@content is text/html 27140 1436499350  nginx/1.7.7



getprint($url)

Get and print a document identified by a URL. The document is printed to the selected default filehandle for output (normally STDOUT) as data is received from the network.  If 

the request fails,then the
status code and message are printed on STDERR.  The return value is the HTTP response code.


get和打印 URL 的document, dodument输出到标准输出,如果请求失败,会返回状态code到错误输出 返回值是 响应码


</body>
</html>@content is 200
[root@dr-mysql01 test]# cat e3.pl 
 use LWP::Simple;
        @content = getprint("http://www.zjcap.cn/");
        die "Couldn't get it!" unless defined @content;
        print "@content is @contentn";


[root@dr-mysql01 test]# perl e3.pl 
500 Can't connect to www.zjcap1.cn:80 (connect: Connection timed out) <URL:http://www.zjcap1.cn/>
@content is 500
You have mail in /var/spool/mail/root





返回状态码:

[root@master test]# cat 1.pl 
 use LWP::Simple;
my $code = getstore('http://zjcap.cn','a.txt');
print "$code is $coden";

[root@master test]# perl 1.pl 
$code is 200



根据返回码判断:

[root@master test]# cat 1.pl 
 use LWP::Simple;
my $code = getstore('http://zjcap.cn','a.txt');
print "$code is $coden";
if (is_success($code)){print "can openn"}
else{print "errorn"};
[root@master test]# perl 1.pl 
$code is 200
can open
[root@master test]# vi 1.pl 
[root@master test]# cat 1.pl 
 use LWP::Simple;
my $code = getstore('http://zjcap.cn1','a.txt');
print "$code is $coden";
if (is_success($code)){print "can openn"}
else{print "errorn"};
[root@master test]# perl 1.pl 
$code is 500
error

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读