參考資料:

HTTP://boyun.sh.cn/blog/?p=1042 抓取腳本總結 亂碼




總結:

 

a. 在 XP 下還有一種安裝模組的方式:
1 、進入 cmd
2 、輸入 perl -MCPAN -e shell ,然後 install File::Find (你要安裝的模組)即可自動下載、安裝模組。如果安 裝本模組需要其他一些模組的支援,還能查找是否安裝了這些模組並提示安裝。第一次執行時可能需要執行一些相關的配置

 

或者:在dos命令列下啟動ppm 
ppm 

 

獲得説明 
PPM>h 

 

列出ActiveState網站上所有為Perl模組的清單。 
PPM>search 

 

安裝模組 
PPM>install DBI 
自動完成DBI模組從下載到安裝的全過程。 

 

退出 
PPM>q 





b. =~ ( =~ 運算子並不進行賦值,它只是取出右邊的運算子,並使它對左邊的變數進行操作)

 

c . s/searchpartten/replacement/( 替換操作 ) 如: $i=~s/<span class= "itemName" >//;

 

d . 特殊變數 $& : 與上個格式匹配的字串,

 

更多請參見:HTTP://blog.csdn.net/zll01/archive/2009/09/04/4520087.aspx

 

e .檔打開寫入操作




原始程式碼:




#!/usr/bin/perl
#use strict;
use LWP::Simple;
use HTML::HeadParser;
use HTML::FormatText::WithLinks;
use Encode qw/encode decode/;
use XML::Generator;

 

my $c = get( 'http://www.nuan.gr.jp/sf/Category.do?catID=10249445' );
encode( "shift-jis" , $c);

 

while ( $c =~ m!<span class= "itemName[^>].*?</span>!g) {
push @itemName,$&;
}
$_ = @itemName;
while ( $c =~ m!<span class= "price[^>].*?</span>!g) {
push @price,$&;
}
my $name;
my $price;
foreach $i(@itemName) {
encode( "utf-8" , $i);
$i=~s/<span class= "itemName" >//;
$i=~s/<\/span>//;
$name=$i=~s/\[Women\]&nbsp;//;
$name=$i;
}
foreach $p(@price) {
encode( "utf-8" , $p);
$p=~s/<span class= "price" >//;
$p=~s/<\/span>//;
$price=$p;
}

 

my ($xml,$output);
$xml = new XML::Generator ( 'conformance' => 'strict' ,
'escape' => 'always' ,
'pretty' => 2 );
$output=$xml->products($xml->name($name),$xml->price($price));
#encode("utf-8", $output);
#print $xml->xmldecl, $output;
open(FILE, ">c://products.xml" )||die "cannot open file\n " ;
print FILE $output;
close FILE;






项目需求:

 

Time To Complete: 2 days

 

<!-- [if !supportLists]-->1. <!-- [endif]-->建立网络爬虫,每天定时抓取页面信息,产生一个 XML 文件

 

<!-- [if !supportLists]-->2. <!-- [endif]-->XML 文件包括以下内容:
- 产品名称

 

- 产品价格

 

- 产品描述

 

- 产品编号( ID )

 

- 产品最后更新时间

 

- 爬虫抓取时间

 

3. 建立配置文件,可以设置抓取的 URL 和抓取时间

 

4. 建立錯誤日誌,記錄抓取錯誤的具體內容(網站,產品,時間,錯誤原因等)




其他考量:

 

<!-- [if !supportLists]-->1. <!-- [endif]-->對系統資源的消耗

 

<!-- [if !supportLists]-->2. <!-- [endif]-->爬蟲效率

 

<!-- [if !supportLists]-->3. <!-- [endif]-->擴充性 (應該做成模組性,以後專案可以利用此模組)






抓取頁面清單:

 

1 、(化妝品)

 

HTTP://www.dhc.co.jp/main/main.jsp

 

2 、(電器)

 

HTTP://www.asahi.com/shopping/electric_av/
arrow
arrow
    全站熱搜

    戮克 發表在 痞客邦 留言(0) 人氣()