作者woominin (没事就好)
看板PHP
标题Re: [请益] parser 文字
时间Sun Sep 28 13:46:02 2014
原文恕删
想请问前辈们
小弟在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://webptt.com/cn.aspx?n=bbs/PHP/M.1411883164.A.113.html
1F:→ bibo9901: parse (v) 和 parser (n) 真的那麽难分吗@@ 09/28 14:02
2F:→ woominin: 不懂什麽意思 - - 09/28 14:14
3F:推 bency: 是一个动词(parse)跟名词(parser)的概念 09/28 15:25
4F:→ KawasumiMai: 就好像你去teach一个人跟你去teacher一个人 09/28 16:07
5F:→ KawasumiMai: 後面不会觉得怪怪的吗 09/28 16:07
6F:推 hit1205: $v 在这里是一个 DOMElement,但你要转换编码的是里头的 09/28 22:08
7F:→ hit1205: 字,比如说你要转 $v->getAttribute('title'); 的话 09/28 22:09
8F:→ hit1205: 那应该是直接转 $v->getAttribute('title');,而不是 09/28 22:09
9F:→ hit1205: 转 $v 本身,因为 mb_convert_encoding 是转字串用的 09/28 22:09
10F:→ hit1205: 如果是要取内文请用 $v->textContent (PHP文件似乎没提XD 09/28 22:16
11F:→ hit1205: 另外,你读的这网站本来就是 UTF-8 了,所以其实不用转码 09/28 22:16