Re: [請益] parser 文字

看板PHP作者 (沒事就好)時間9年前 (2014/09/28 13:46), 編輯推噓2(209)
留言11則, 5人參與, 最新討論串4/4 (看更多)
原文恕刪 想請問前輩們 小弟在parser網頁遇到一個新的問題 就是用原本的 simple_parser_dom的工具來parser http://tour.taitung.gov.tw/zh-tw/Home/Index 會出錯 問題1 : 如何解 再來小弟到處研究了一下 用了另一個 curl <?php # Use the Curl extension to query Google and get back a page of results $url = "http://tour.taitung.gov.tw/zh-tw/Home/Index"; $ch = curl_init(); $timeout = 5; curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout); $html = curl_exec($ch); curl_close($ch); # Create a DOM parser object $dom = new DOMDocument(); # Parse the HTML from Google. # The @ before the method call suppresses any warnings that # loadHTML might throw because of invalid HTML in the page. @$dom->loadHTML($html); # Iterate over all the <a> tags foreach($dom->getElementsByTagName('a') as $link) { # Show the <a href> echo $link->getAttribute('href'); echo "<br />"; } foreach($dom->getElementsByTagName('a') as $v) { echo $v->getAttribute('title'); echo "<br />"; } ?> 用上面的語法 是parser出來了,不過parser回來的字是亂碼 試著加入 $v = mb_convert_encoding($v,"BIG5","UTF-8"); 結果會出錯 請教這如何解呢 ? -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 49.158.112.110 ※ 文章網址: http://www.ptt.cc/bbs/PHP/M.1411883164.A.113.html

09/28 14:02, , 1F
parse (v) 和 parser (n) 真的那麼難分嗎@@
09/28 14:02, 1F

09/28 14:14, , 2F
不懂什麼意思 - -
09/28 14:14, 2F

09/28 15:25, , 3F
是一個動詞(parse)跟名詞(parser)的概念
09/28 15:25, 3F

09/28 16:07, , 4F
就好像你去teach一個人跟你去teacher一個人
09/28 16:07, 4F

09/28 16:07, , 5F
後面不會覺得怪怪的嗎
09/28 16:07, 5F

09/28 22:08, , 6F
$v 在這裡是一個 DOMElement,但你要轉換編碼的是裡頭的
09/28 22:08, 6F

09/28 22:09, , 7F
字,比如說你要轉 $v->getAttribute('title'); 的話
09/28 22:09, 7F

09/28 22:09, , 8F
那應該是直接轉 $v->getAttribute('title');,而不是
09/28 22:09, 8F

09/28 22:09, , 9F
轉 $v 本身,因為 mb_convert_encoding 是轉字串用的
09/28 22:09, 9F

09/28 22:16, , 10F
如果是要取內文請用 $v->textContent (PHP文件似乎沒提XD
09/28 22:16, 10F

09/28 22:16, , 11F
另外,你讀的這網站本來就是 UTF-8 了,所以其實不用轉碼
09/28 22:16, 11F
文章代碼(AID): #1K9w2S4J (PHP)
文章代碼(AID): #1K9w2S4J (PHP)