[mw_shl_code=php,true]<?php " V9 L( I, j8 r% q" x
set_time_limit(0);//設置程序執行時間 / H$ Q- ?& F7 i8 d+ A
header("Content-Type: text/html; charset= utf-8"); //設置頁面的編碼,不設置會亂碼
" M5 V4 X& ]7 N7 |
+ A8 V3 u3 G( S$url="http://www.luosuo.net/old/forum-4-1.html"; # Z" {& F; [5 v* p; }8 m) z
$ch = curl_init($url);//初始化 2 q: \8 p+ a7 [( r
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);//返回的內容我們是作為變量儲存, 而不輸出,所以這裡選1.
5 \: ~- h3 P& L; p% A$contents = curl_exec($ch);// 運行cURL,請求網頁
8 x' K. j! K; U6 W& w) Z- D! `preg_match_all('{<a href="([^"]+)" target="_blank">(.*)</a>}',$contents,$matches);//正則匹配想要的內容
- F( _1 x. G4 o% |curl_close($ch);// 關閉URL請求
: ^; e/ t0 d6 l( A6 \$ Xprint_r($matches);
4 e6 K7 I+ Z. Q: Z; B! }for ($i=0;$i<count($matches[1]);$i++)//for循環結構很常用,這裡的作用是循環記錄總數的的次數,每次循環中可以任意處理採集到的內容 : A: f# g9 h. x0 r
{ ; ]! x5 r' r+ {5 u Q
$title = $matches[2][$i]; 4 m! \- K! c. a: O
$geturl = $matches[1][$i];
, R& l) Q5 t/ O2 wecho "我要的標題是:" . $title. "<br/>" . "對應的網址是:" . $geturl . "<br/><br/><br/>";
: W* c/ v! b0 {1 R z4 b} 5 B" d, m& R2 ^. G
?>[/mw_shl_code]
: @* d) g% D. v$ S' ]3 Y: j7 z2 m( Y" C9 I- X
這是我測試的代碼,怎麼出現了這個
, u3 C( x* j* X! g! a/ o# d- f
4 h& T- c: y2 q' A6 _% M" o+ p" I
2 \: a2 E a% _6 R |