2017-10-02 3 views
0

개인 프로젝트의 EPG 데이터를 긁기 위해 Simple HTML DOM을 사용하고 있습니다.단순 HTML 돔을 사용할 때 중복되는 것을 피하십시오

현재 코드가 각 채널의 데이터를 스크랩하고 json 파일에 덤프합니다. 내 데이터를 필터링하여 모든 데이터를 스크랩했습니다. $Channels은 내 스트림을 추가하는 것과 함께 특별히 요청한 항목에만 데이터를 스크랩합니다. 다음을 사용하여 링크 ...

$channels = array(
     "ITV1 London" => "URL 1", 
); 

내가 출력 된 JSON 파일에서 중복되는 각 채널의 데이터를 피할 수있는 방법을 알아낼 수 없습니다

. $channels을 요청해야하므로 마지막 출력에 제시된 데이터와 최종 출력에 추가되는 자체 링크를 필터링 할 수 있습니다.

if ($channels[$channel_name]) { 
      $channel = array(); 

Screenshot of working code

<?php 

// Include the php dom parser 
include_once 'simple_html_dom.php'; 

header('Content-type: application/json'); 

// Create DOM from URL or file 

$curl = curl_init(); 
curl_setopt($curl, CURLOPT_HEADER, 0); 
curl_setopt($curl, CURLOPT_RETURNTRANSFER,1); 
curl_setopt($curl, CURLOPT_URL, "http://tv24.co.uk"); 
$html=curl_exec($curl); 
$dom = new simple_html_dom(null, true, true); 
$html=$dom->load($html, true, true); 

$channels = array(
    "ITV1 London" => "URL 1" 
); 

$data = array(); 

foreach($html->find('section div') as $ul) 
{ 
    foreach($ul->find('div.channel-wrapper') as $show) { 

     $channel_name = $show->find('h2.name')[0]->plaintext; 

     if ($channels[$channel_name]) { 
      $channel = array(); 

      $channel['channel'] =$channel_name ; 
      $channel['logo'] = $show->find('span.logo img')[0]->src; 
      $channel['thumb'] = explode("'", $show->find('div.program')[0]->style)[1]; 
      $channel['on-now'] = $show->find('span.title a')[0]->plaintext; 
      $channel['on-now-time'] = $show->find('span.time')[0]->plaintext; 
      $channel['on-now-description'] = $show->find('span.description')[0]->plaintext; 
      $channel['up-next'] = $show->find('span.title a')[1]->plaintext; 
      $channel['up-next-time'] = $show->find('span.time')[1]->plaintext; 
      $channel['stream'] = $channels[$channel_name]; 

      $data['data'][] = $channel; 
     } 

    } 

} 
echo json_encode($data); 

$myFile = "output.json"; 
$fh = fopen($myFile, 'w') or die("error"); 
$stringData = json_encode($data); 
fwrite($fh, $stringData); 
fclose($fh); 

?> 

답변

0

채널

$data['data'][$channel_name] = $channel; 

에 대한 연관 배열을 사용하여 전체 코드 이전 항목을 새 항목으로 덮어 씁니다. 똑똑한 방법은 채널 배열을 유지하고 이미 수행 한 처리 과정을 건너 뛸 수 있습니다.

$channels[] = $channel_name; 

.... 

if (in_array($channel_name, $channels)) 
    // skip this one 
관련 문제