Re: [請益] 抓取網頁html原始碼
※ 引述《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() 把 "<>" 轉成 "<>"
: $tok = strtok($html, "<h3>posting history</h3>");
: echo $tok;
: ?>
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 140.119.199.121
討論串 (同標題文章)
PHP 近期熱門文章
PTT數位生活區 即時熱門文章