Re: [請益] PHP切割遠端網頁原始碼
※ 引述《troy1005 (小新)》之銘言:
: 詢問的問題是我想切割這個網址:http://tinyurl.com/6c6njg8
: 找出無名小站文章的網址出來 在原始碼中的第一個內容是
: http%3a//www.wretch.cc/blog/love52071835/1953378"
: ------------------以下是我的程式碼------------------
: $url = 目標網址
: $fp = file_get_contents($url);
: $url_count = preg_match_all
: ( '/http%3a\:\/\/www\.wretch\.cc\/blog\/(.*)\/(.*)/',$fp,$match);
: $i=0;
: while($i<10){
: $title = $match[i];
: echo "Title[$i]: $title </br>";
: $i++;
: }
: 這樣執行結果一直都沒有東西 想請問問題可能是出在哪裡?
: 另外不太懂(.*)跟(.+?)的分別 希望可以大概說明一下 謝謝
: 有違反版規 麻煩請告知
請愛用
function get_page($url,$PostData=false) {
$user_agent = 'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0;
Trident/4.0; SLCC1; .NET CLR 2.0.50727; .NET CLR 3.0.04506)';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url); //設定截取網址
curl_setopt($ch, CURLOPT_USERAGENT, $user_agent); //瀏覽器的user agent
curl_setopt($ch, CURLOPT_HEADER, 0); //是否截取header的資訊
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); //是否抓取轉址
if($PostData != false) curl_setopt($ch,CURLOPT_POSTFIELDS,$PostData); // 將
post資料塞入
$html = curl_exec($ch);
curl_close($ch);
return $html;
}
然後關於抓取資料
preg_match_all('/<tr[^>]*class=\'basictxt\'[^>]*>(.*?)<\/tr>/si',$html,$match);
這上面的意思是 我要抓 class='basictxt' 的 tr 裡面的東西
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 114.32.228.158
討論串 (同標題文章)