[mw_shl_code=php,true]<?php - g; U4 L$ H. l$ O+ J- Z
set_time_limit(0);//設置程序執行時間 5 ]8 H" B6 [7 i( O$ }. W
header("Content-Type: text/html; charset= utf-8"); //設置頁面的編碼,不設置會亂碼
, e$ E" n( V* q( d
4 v$ A8 s" x8 C i- T1 y$url="http://www.luosuo.net/old/forum-4-1.html"; 4 Y6 c$ C6 z. |9 {
$ch = curl_init($url);//初始化 , Y( R" x6 V k7 y: ]2 F' v
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);//返回的內容我們是作為變量儲存, 而不輸出,所以這裡選1. # M, `7 I9 c `4 X1 s$ H
$contents = curl_exec($ch);// 運行cURL,請求網頁
6 ^+ j: W/ g5 C: h8 @preg_match_all('{<a href="([^"]+)" target="_blank">(.*)</a>}',$contents,$matches);//正則匹配想要的內容
+ U1 u, d$ O8 b7 vcurl_close($ch);// 關閉URL請求
4 i4 w+ h! p+ @' F' ~print_r($matches);
n# S$ ]2 c3 Jfor ($i=0;$i<count($matches[1]);$i++)//for循環結構很常用,這裡的作用是循環記錄總數的的次數,每次循環中可以任意處理採集到的內容 # M2 G( L2 f7 d" n
{ t7 h/ @7 Y, o! b+ Z
$title = $matches[2][$i];
% L! f& j( u2 ~1 e$geturl = $matches[1][$i];
9 e# k$ A' j5 ^' kecho "我要的標題是:" . $title. "<br/>" . "對應的網址是:" . $geturl . "<br/><br/><br/>";
; M, c# X% _& F$ f% {} % k d, ?7 d4 F b8 a; R3 l; ?
?>[/mw_shl_code]: ?3 b4 u0 @, l! v8 w
# s( W2 T. {5 C這是我測試的代碼,怎麼出現了這個) S- i& ]/ T/ _/ z, f* }" f, T4 C
9 s: ^/ h$ K& d' C$ x% M& F7 E
- K, R, m. q$ V* [, v$ t |