我是 Perl 初学者,我热衷于使用 Perl 进行网页抓取。花了几个小时后,我编写了下面的代码,用于从yell.com
. 该脚本运行良好,我成功收集了一条记录(第 1 页的 1/15)。
我需要您的宝贵建议,关于如何一次将第一页中的所有十家公司都刮掉,以便我可以转到其他页面的数据。
use strict;
use Data::Dumper;
use LWP::Simple; # from CPAN
use JSON qw( decode_json ); # from CPAN
use WWW::Mechanize;
my $mech = WWW::Mechanize->new();
my $header = "company_name|Address|Telphone";
open (CH, ">output.csv");
print CH "$header\n";
my $url = "http://www.yell.com/ucs/UcsSearchAction.do?keywords=Engineering+consulatants&location=United+Kingdom&scrambleSeed=13724563&searchType=&M=&bandedclarifyResults=&ssm=1";
$mech->get($url);
my $con = $mech->content();
my $res = "";
############ for company name ##########
if ( $con =~ /<a data-omniture="LIST:COMPANYNAME" href="\/biz\/ross-davy-associates-grimsby-901271213\/" itemprop="name">(.*?)<\/a>/is ) {
$res = $1;
}
else {
$res = "Not_Match";
}
############### for address #########
my ($add1, $add2, $add3, $add4, $add) = ("", "", "", "", "");
if ( $con =~ /<span itemprop="streetAddress">(.*?)<\/span> <span itemprop="addressLocality">(.*?)<\/span> , <span itemprop="postalCode">(.*?)<\/span> , <span itemprop="addressRegion">(.*?)<\/span>/is ) {
$add1 = $1;
$add2 = $2;
$add3 = $3;
$add4 = $4;
$add = $1.$2.$3.$$;
}
else {
$add = "Not_Match";
}
########### telephone ##########
my $tel="";
if ( $con =~ /<li data-company-item="telephone" class="last"> Tel: <strong>(.*?)<\/strong> <\/li>/is ) {
$tel = $1;
}
else {
$tel = "Not_Match";
}
print "==$res===$add===$tel==\n";
print CH "$res|$add|$tel\n";