#!/usr/bin/perl -w
# 程序代码只是简单说明获取解析网页内容及匹配模式使用的例子,逻辑并不是很严密
use utf8;
binmode(STDIN, ‘:encoding(utf8)‘);
binmode(STDOUT, ‘:encoding(utf8)‘);
binmode(STDERR, ‘:encoding(utf8)‘);
use LWP::UserAgent;
use Encode;
sub get_data{
if(@_ != 1)
{
print "error, need an argument\n";
return -1;
}
my $user_agent = LWP::UserAgent->new;
my $des = "http://tool.chinaz.com/Same/";
my $url_ip = $_[0];
my $response = $user_agent->post($des, [ ‘s‘ => $url_ip]);
if ( $response->is_success ) {
my $doc = decode("utf8", $response->content);
while($doc =~ m!<div id="contenthtml">\r\n([^\r\n]*)</div>!sg) {
my $tmp = $1;
$tmp =~ s!<ul>(.*)</ul>!$1!g;
$tmp = $1;
while($tmp =~ s!<li><span>[^<>]*</span>\s<a\s([^<>]*)>[^<>]*</a></li>!$1!){
my $str = $1;
$str =~ s!‘([^‘]*)‘!$1 !g;
print "$str\n";
}
}
}
}
&get_data("61.135.169.125");
exit;
perl post函数获取网页内容及解析数据的例子,布布扣,bubuko.com
原文地址:http://www.cnblogs.com/morris-tech/p/3864314.html