當前位置:才華齋>計算機>php語言>

PHP可以靈活配置使用的採集器

php語言 閱讀(1.88W)

PHP可以靈活配置使用的採集器?就跟隨本站小編一起去了解下吧,想了解更多相關資訊請持續關注我們應屆畢業生考試網!

PHP可以靈活配置使用的採集器

程式碼:

<?php

/**

* 可以靈活配置使用的採集器

* 作者:Rain

* 建立時間:2015-02-03 15:17:30

* 版本資訊:V1.0

*/

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

//資料庫的相關配置資訊,請根據您的資料庫資訊進行配置

define('DB_HOST', 'localhost');

define('DB_USER', 'root');

define('DB_PWD', 'test123456');

define('DB_NAME', 'test_dbname');

define('DB_CHARSET', 'utf8');

define('TABLE_NAME', 'tb_book');

//end

//網站資訊相關的配置,請根據具體需要採集的網站內容資訊進行配置

define('WEB_CHARSET', 'gbk');

//變動的引數,使用%d進行替換,只支援數值形式的變動

define('WEB_LIST_URL', '_%');

//分頁的條數

define('PAGE_COUNT', 14);

//從哪個頁面開始抓取

define('PAGE_START', 1);

//內容頁的URL,使用正則模式,必須包含/,例如://xuefu2008/article/details/(d)+/i

define('WEB_CONTENT_URL_REG', '//book/(d)+');

//網站域名HOST資訊,不包含末尾的/,例如:

define('WEB_HOST', '');

//列表頁內容的精準定位,用來大致抓取一個列表頁的內容顯示模組位置,使用正則進行定位

define('WEB_LIST_POSTION', '/book_name.gif(.*?)<tdswidth="15%"snowrap>/i');

//end

//微調引數,通常不修改也不會影響您的正常使用

define('SLEEP_TIME', 1);

define('IS_DEBUG', false);

define('INSERT_DB', true);

//內容的輸出速度,單位:秒

define('OUTPUT_SPEED', 1);

//end

//需要過濾刪除的文字,根據採集的網站型別進行設定,不區分大小寫

$text_filter = array(

'- 中華電腦書庫' => '',

'_電腦電子書' => '',

'_電腦書籍' => '',

'下載' => '',

);

//表結構對映的配置

$table_mapping = array(

//表字段名稱 => 獲取該欄位的正則表示式,非空欄位都必須在此設定對映關係,常量值請直接填寫具體對應的值,無需使用正則

'size' => '/軟體大小.*?000000>(.*?)</font>/i',

'logo' => 'http://www.94cto.com/index/uploads/images/20150105/0b8461910de101cc51a07684cdab797e.jpg',

'field1' => '/<title>(.*?)</title>/i',

'field2' => '/軟體簡介.*?000000>(.*?)</font>/i',

'field3' => '1',

'field4' => '1',

'field5' => '1',

'field6' => '電子書,計算機,影象,圖形',

'platform' => 'window/Linux',

'ishot' => '1',

'agreement' => '免費',

'downurl' => '/(/down?id=.*?)"/i',

'istop' => '1',

);

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

$ga = new Gather();

$ga->run();

class Gather

{

public function __construct()

{

$this->init_check();

}

public function run()

{

global $table_mapping, $text_filter;

for ($page = PAGE_START; $page <= PAGE_COUNT; $page++)

{

$this->write('開始採集列表第'.$page.'頁的內容...');

$list_content = $this->get(sprintf(WEB_LIST_URL, $page));

if (empty($list_content))

{

$this->write('抓取的列表頁的內容為空,所以過濾掉');

continue;

}

$list_content = str_replace("r", '', $list_content);

$list_content = str_replace("n", '', $list_content);

//精準定位要抓取的模組內容

if (!preg_match(WEB_LIST_POSTION, $list_content, $list_search))

{

$this->write('精準匹配列表頁的內容失敗,所以過濾掉');

continue;

}

if (isset($list_search[1]))

$list_content = $list_search[1];

else

$list_content = $list_search[0];

//end

preg_match_all(WEB_CONTENT_URL_REG, $list_content, $match);

if (is_array($match[0]) && !empty($match[0]))

{

$this->write('當前的'列表頁面,總共匹配到:'t($match[0]).'個內容頁');

foreach ($match[0] as $val)

{