php와 파싱을 처음 접하는 초보입니다. 인터넷에 있는 소스와 질문으로 제가 원하는 부분을 하나하나 해결해 가는 와중에 문제가 있어 조언을 얻고자 합니다.
1. 원소스
-. 가져오고자 하는 게시판에서 하나의 목록,shopCode=12032에서 원하는 항목을 가져오는 소스이고 제가 원하는 대로 문제없이 작동합니다.
본문 내 주석 부분이 제대로 출력됩니다.
// 아래 값이 제대로 출력됩니다.
echo $data['item'][1][1]. "--" ;
[ 출력결과 ] 울산구영점--10월 21일 / 10월 28일--http://www.publishchanger.com/Viewer/p1
<?php include_once 'Snoopy.class.php'; $snoopy=new snoopy; $snoopy->fetch('http://gssuper.gsretail.com/gssuper/ko/market-info/find-view?shopCode=12032'); $result = $snoopy->results; preg_match('/<h4 class="tit">(.*?)<\/h4>/is', $result, $title); $data['store'] = $title[1]; preg_match('/<div class="mk_right">(.*?)<\/div>/is', $result, $html); preg_match_all('/<li><strong>(.*?)<\/li>/is', $html[1], $el); foreach($el[1] as $rs){ $rs = preg_replace('/\r\n|\r|\n/','',$rs); $temp = explode('</strong><p>',$rs); if(preg_match('/배송제도|찾아오시는길/', $temp[0])){ $temp[0] = str_replace('배송제도</strong>', '배송제도', $temp[0]); $temp2 = explode("<p>", $temp[0]); $temp[0] = strip_tags(trim($temp2[0])); $temp[1] = str_replace('</p>', '', trim($temp2[1])); }else{ $temp[0] = strip_tags($temp[0]); $temp[1] = strip_tags($temp[1]); } $data['item'][] = $temp; } preg_match('/<div class="mk_btn">(.*?)<\/div>/is', $result, $linkdata); preg_match_all('/onclick="searchLeaflet(.*?)"/is', $linkdata[1], $link); #print_r($link[1]); preg_match_all("/\'(.*?)\'/is", $link[1][0], $num); #print_r($num[1]); $data['link'] = $num[1][1]; //print_r($data)."<br />"; echo $data['store']. "--" ; // 아래 값이 제대로 출력됩니다. echo $data['item'][1][1]. "--" ; echo $data['link']."<br />"; ?>
2. 추가 수정 소스
-. 가져오고자 하는 게시판에서 전체 목록에서 제가 원하는 항목을 가져오는 소스입니다.
-. 아래 소스 상에서는 배열로 3개의 목록만 가져와서 제가 원하는 항목을 뽑는걸로 임시 테스트 했습니다.
$x=array("12032","11250","13087"); 테스트에 성공하면 전체 게시판의 shopCode=????? 약 300개를 수동 조사해서 값을 넣을 생각입니다.
제가 반복문을 하나 더 추가 하고 테스트를 해 보면 위에서
echo $data['item'][1][1]. "--" ; 의 값이 제일 첫번째( 울산구영점)의 값을 두번째, 세번째 에서 그래도 가져오는 문제점이 발생합니다.
울산구영점--10월 21일 / 10월 28일--http://www.publishchanger.com/Viewer/p1
강남대치점--10월 21일 / 10월 28일--http://www.publishchanger.com/Viewer/p0(실제 값 : 07월 10일 / 07월 24일)
강남영동점--10월 21일 / 10월 28일--http://www.publishchanger.com/Viewer/p5(실제 값 : 07월 10일 / 07월 24일)
<?php // 제가 추가한 부분입니다. $x=array("12032","11250","13087"); foreach ($x as $value) { // 추가 끝 include_once 'Snoopy.class.php'; $snoopy=new snoopy; // 제가 추가한 부분입니다. shopCode='.$value) $snoopy->fetch('http://gssuper.gsretail.com/gssuper/ko/market-info/find-view?shopCode='.$value); // 추가 끝 $result = $snoopy->results; preg_match('/<h4 class="tit">(.*?)<\/h4>/is', $result, $title); $data['store'] = $title[1]; preg_match('/<div class="mk_right">(.*?)<\/div>/is', $result, $html); preg_match_all('/<li><strong>(.*?)<\/li>/is', $html[1], $el); foreach($el[1] as $rs){ $rs = preg_replace('/\r\n|\r|\n/','',$rs); $temp = explode('</strong><p>',$rs); if(preg_match('/배송제도|찾아오시는길/', $temp[0])){ $temp[0] = str_replace('배송제도</strong>', '배송제도', $temp[0]); $temp2 = explode("<p>", $temp[0]); $temp[0] = strip_tags(trim($temp2[0])); $temp[1] = str_replace('</p>', '', trim($temp2[1])); }else{ $temp[0] = strip_tags($temp[0]); $temp[1] = strip_tags($temp[1]); } $data['item'][] = $temp; } preg_match('/<div class="mk_btn">(.*?)<\/div>/is', $result, $linkdata); preg_match_all('/onclick="searchLeaflet(.*?)"/is', $linkdata[1], $link); #print_r($link[1]); preg_match_all("/\'(.*?)\'/is", $link[1][0], $num); #print_r($num[1]); $data['link'] = $num[1][1]; //print_r($data)."<br />"; echo $data['store']. "--" ; // 이 부분에서 값을 제대로 가져오지 못하는 문제가 발생합니다. echo $data['item'][1][1]. "--" ; echo $data['link']."<br />"; } ?>
데이터를 잘못 가져오는 문제가 제가 추가한 반복문을 잘못 쓴건지 조언 부탁드립니다.
그리고 디비 연동없이 php 문 내에서 전체 리스트 중 원하는 항목만 파싱 값을 얻기 위해
$x=array("12032","11250","13087"); 값을 약 300개 정도 넣고 반복을 시키면 문제가 없을 지 원래 전체게시판의 특정 항목을 파싱해 올때 이런 식으로 하는게 맞는지도 궁금합니다.
<?php
$x=array("12032","11250","13087");
foreach ($x as $value)
{
......
$snoopy->fetch('http://gssuper.gsretail.com/gssuper/ko/market-info/find-view?shopCode='.$value);
....
foreach($el[1] as $rs){
....
}
....
}
?>