perl在网页中抓取图片 binmode(转帖)

perl在网页中抓取图片 binmode

    今天在天涯上闲逛的时候,突然一个网页上面楼主贴了很多图片,都是满漂亮的,于是就想把这些图片都下载到本地的电脑上。以前用perl的LWP::Simple模块主要是提前里面的一些文本信息,基本上没碰到过什么障碍,不过这次下载图片可是第一次。利用以前下载文本信息的程序处理时,发现得到的图片内容都是二进制类型的,也就是这些get($url)出的结果是二进制的。后来在网上搜索到了一些解决方法,试了一下的确能够解决问题!

 

方法一:利用binmode函数将文件句柄转化下可以接受二进制的形式。

 

binmode 功能:

 

binmode FILEHANDLE

     Arranges for FILEHANDLE to be read or written in "binary" or "text" mode on systems where the run-time libraries distinguish between binary and text files. If FILEHANDLE is an expression_r, the value is taken as the name of the filehandle. Returns true on success, otherwise it returns undef and sets $! (errno).

 

codes:

 

#!usr/bin/perl

use strict;

use LWP::Simple;

 

my $url="http::/../abc.jpg";

open FILE,">abc.jpg" or die "$!";

my $outcome=get ($url);

binmode(FILE);

print FILE $outcome;                    # input your want picture into the file named abc.jpg

 

 

方法二: 利用函数getstore()

 

getstore()功能:

 

见文档!

 

code:

 

#!usr/bin/perl

use strict;

use LWP::Simple;

 

my $url="http::/../abc.jpg";

getstore($url,"abc.jpg") or die "get picture failed!";

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值