Re: [請益] 抓取網頁html原始碼

看板PHP作者 (O⊥M)時間18年前 (2007/05/16 17:19), 編輯推噓0(000)
留言0則, 0人參與, 最新討論串3/4 (看更多)
※ 引述《NCK (守備範圍:18~25歲)》之銘言: : <?php : $url = $_GET['URL'].$_POST['words']; : $ch = curl_init(); //初始化curl,要準備開始抓網頁 : curl_setopt($ch, CURLOPT_URL, $url); //告訴url要抓的是第一行的網頁 : curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); //不要將抓回來的網頁秀到螢幕上, : //等等我們要繼續分析 : $content = curl_exec($ch); //抓吧,然後將資料存到$content ^^^^^^^^^^^^^^^^^^^^^^^^^ 其實 $content 的內容就是原始的 html 字串, 所以應該針對 $content 做 parse 才對 : curl_close($ch); : $html = htmlentities($content); ^^^^^^^^^^^^^^^^^^^^^^ 這麼做則是去除 html 的"特性"--把"標籤"轉為一般的字串。 如果你要做的是把 html 原始碼、或許幫標籤上色之後. 完整地秀在網頁上... 那才需要用 htmlentities() 把 "<>" 轉成 "&lt;&gt;" : $tok = strtok($html, "<h3>posting history</h3>"); : echo $tok; : ?> -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 140.119.199.121
文章代碼(AID): #16Iioh-G (PHP)
文章代碼(AID): #16Iioh-G (PHP)