今天在天涯上闲逛的时候,突然一个网页上面楼主贴了很多图片,都是满漂亮的,于是就想把这些图片都下载到本地的电脑上。以前用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!";
总结以上是内存溢出为你收集整理的perl在网页中抓取图片 binmode(转帖)全部内容,希望文章能够帮你解决perl在网页中抓取图片 binmode(转帖)所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)