PHP+Snoopy 파싱으로 안될때에 중고나라 파싱 예제
어디까지나 파싱예제입니다.
스누피 클래스를 사용하지 않고 fsockopen 으로 웹페이지 긁는 방법입니다.
function getGB($server, $jsp, $url) {
$map_query = str_replace(" ","%20",$url);
$pquery = $jsp;
$fp = fsockopen ($server, 80, $errno, $errstr, 10);
if (!$fp) {
echo "$errstr ($errno)";
} else {
fputs($fp, "GET {$pquery}?");
fputs($fp, $url);
fputs($fp, " HTTP/1.1\r\n");
fputs($fp, "Host: {$server}\r\n");
fputs($fp, "Connection: Close\r\n\r\n");
$header = "";
while (!feof($fp)) {
$out = fgets ($fp,512);
if (trim($out) == "") {
break;
}
$header .= $out;
}
$mapbody = "";
while (!feof($fp)) {
$out = fgets ($fp,512);
$mapbody .= $out;
}
fclose ($fp);
}
return $mapbody;
}
<li id="title">naver : 중고나라</li>
<?
$tmp=getGB("cafe.naver.com","/ArticleList.nhn","search.boardtype=L&search.menuid=334&search.questionTab=A&search.clubid=10050146&search.totalCount=151&search.page=1");
//echo nl2br(htmlspecialchars($tmp));<br />
//echo $tmp;
$tmp=explode("<div class=\"article-board m-tcol-c\">",$tmp);
$item=explode("<span class=\"aaa\">",$tmp[1]);
for ($i=1;$i<count($item) && $i<5;$i++) {
$tmp=explode("<input",$item[$i]);
$ttt=explode("class=\"m-tcol-c\">",$tmp[0]);
$ttt[0]=str_replace("<a href='/ArticleRead.nhn","<a target='_blank' href=\"http://cafe.naver.com/ArticleRead.nhn",$ttt[0]);
$title[$i]=$ttt[0]."class=\"m-tcol-c\">".strip_tags($ttt[1])."</a>";
//$title[$i]=str_replace("<a href='/ArticleRead.nhn","<a target='_blank' href=\"http://cafe.naver.com/ArticleRead.nhn",$tmp[0]);
}
for ($i=1;$i<count($item) && $i<5;$i++) {
?>
<li>
<nobr style='display:block; overflow:hidden; width:450px;'><?=$title[$i]?></nobr>
</li>
<?
}
?>
<br />