重慶分公司,新征程啟航
為企業提供網站建設、域名注冊、服務器等服務
為企業提供網站建設、域名注冊、服務器等服務
簡單的分了幾個步驟:
成都網站建設公司更懂你!創新互聯只做搜索引擎喜歡的網站!成都網站制作前臺采用搜索引擎認可的DIV+CSS架構,全站HTML靜態,H5開發+CSS3網站,提供:網站建設,微信開發,微信小程序開發,購物商城網站建設,重慶APP軟件開發,域名申請,服務器租售,網站代托管運營,微信公眾號代托管運營。
1、確定采集目標
2、獲取目標遠程頁面內容(curl、file_get_contents)
3、分析頁面html源碼,正則匹配你需要的內容(preg_match、preg_match_all),這一步最為重要,不同頁面正則匹配規則不一樣
4、入庫
1、建議你讀寫數據和下載圖片分開,各用不同的進程完成。
比如說,取數據用get-data.php,下載圖片用get-image.php。
2、多進程的話,php可以簡單的用pcntl_fork()。這樣可以并發多個子進程。
但是我不建議你用fork,我建議你安裝一個gearman worker。這樣你要并發幾個,就啟幾個worker,寫代碼簡單,根本不用在代碼里考慮thread啊,process等等。
3、綜上,解決方案這樣:
(1)安裝gearman worker。
(2)寫一個get-data.php,在crontab里設置它每5分鐘執行一次,只負責讀數據,然后把讀回來的數據一條一條的扔到 gearman worker的隊列里;
然后再寫一個處理數據的腳本作為worker,例如叫process-data.php,這個腳本常駐內存。它作為worker從geraman 隊列里讀出一條一條的數據,然后跟你的數據庫老數據比較,進行你的業務邏輯。如果你要10個并發,那就啟動10個process-data.php好了。處理完后,如果圖片地址有變動需要下載圖片,就把圖片地址扔到 gearman worker的另一個隊列里。
(3)再寫一個download-data.php,作為下載圖片的worker,同樣,你啟動10個20個并發隨便你。這個進程也常駐內存運行,從gearman worker的圖片數據隊列里取數據出來,下載圖片
4、常駐進程的話,就是在代碼里寫個while(true)死循環,讓它一直運行好了。如果怕內存泄露啥的,你可以每循環10萬次退出一下。然后在crontab里設置,每分鐘檢查一下進程有沒有啟動,比如說這樣啟動3個process-data worker進程:
* * * * * flock -xn /tmp/process-data.1.lock -c '/usr/bin/php /process-data.php /dev/null 21'
* * * * * flock -xn /tmp/process-data.2.lock -c '/usr/bin/php /process-data.php /dev/null 21'
* * * * * flock -xn /tmp/process-data.3.lock -c '/usr/bin/php /process-data.php /dev/null 21'
不知道你明白了沒有
其實用PHP來爬會非常方便,主要是PHP的正則表達式功能在搜集頁面連接方面很方便,另外PHP的fopen、file_get_contents以及libcur的函數非常方便的下載網頁內容。
具體處理方式就是建立就一個任務隊列,往隊列里面插入一些種子任務和可以開始爬行,爬行的過程就是循環的從隊列里面提取一個URL,打開后獲取連接插入隊列中,進行相關的保存。隊列可以使用數組實現。
當然PHP作為但線程的東西,慢慢爬還是可以,怕的就是有的URL打不開,會死在那里。
本文承接上面兩篇,本篇中的示例要調用到前兩篇中的函數,做一個簡單的URL采集。一般php采集網絡數據會用file_get_contents、file和cURL。不過據說cURL會比file_get_contents、file更快更專業,更適合采集。今天就試試用cURL來獲取網頁上的所有鏈接。示例如下:
?php
/*
* 使用curl 采集hao123.com下的所有鏈接。
*/
include_once('function.php');
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, '');
// 只需返回HTTP header
curl_setopt($ch, CURLOPT_HEADER, 1);
// 頁面內容我們并不需要
// curl_setopt($ch, CURLOPT_NOBODY, 1);
// 返回結果,而不是輸出它
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$html = curl_exec($ch);
$info = curl_getinfo($ch);
if ($html === false) {
echo "cURL Error: " . curl_error($ch);
}
curl_close($ch);
$linkarr = _striplinks($html);
// 主機部分,補全用
$host = '';
if (is_array($linkarr)) {
foreach ($linkarr as $k = $v) {
$linkresult[$k] = _expandlinks($v, $host);
}
}
printf("p此頁面的所有鏈接為:/ppre%s/pren", var_export($linkresult , true));
?
function.php內容如下(即為上兩篇中兩個函數的合集):
?php
function _striplinks($document) {
preg_match_all("'s*as.*?hrefs*=s*(["'])?(?(1) (.*?)\1 | ([^s]+))'isx", $document, $links);
// catenate the non-empty matches from the conditional subpattern
while (list($key, $val) = each($links[2])) {
if (!empty($val))
$match[] = $val;
} while (list($key, $val) = each($links[3])) {
if (!empty($val))
$match[] = $val;
}
// return the links
return $match;
}
/*===================================================================*
Function: _expandlinks
Purpose: expand each link into a fully qualified URL
Input: $links the links to qualify
$URI the full URI to get the base from
Output: $expandedLinks the expanded links
*===================================================================*/
function _expandlinks($links,$URI)
{
$URI_PARTS = parse_url($URI);
$host = $URI_PARTS["host"];
preg_match("/^[^?]+/",$URI,$match);
$match = preg_replace("|/[^/.]+.[^/.]+$|","",$match[0]);
$match = preg_replace("|/$|","",$match);
$match_part = parse_url($match);
$match_root =
$match_part["scheme"]."://".$match_part["host"];
$search = array( "|^http://".preg_quote($host)."|i",
"|^(/)|i",
"|^(?!http://)(?!mailto:)|i",
"|/./|",
"|/[^/]+/../|"
);
$replace = array( "",
$match_root."/",
$match."/",
"/",
"/"
);
$expandedLinks = preg_replace($search,$replace,$links);
return $expandedLinks;
}
?