2010-12-29 4 views
0

빠른 질문이 있습니다. 다른 블로그에서 기사를 수집하는 PHP 웹 사이트를 구축하려고합니다. 어떻게 PHP 에서이 코드겠습니까? 정규식 선언문이 필요합니까? 내가해야 할 일은 특정 페이지에서 기사를 가져 오는 것입니다. 예 : http://rss.news.yahoo.com/rss/education 아무도 도와 줄 수 있습니까? 고맙습니다.기사를 수집하는 PHP로 웹 사이트를 구축하는 방법은 무엇입니까?

+1

기사 수집으로 무엇을 의미합니까? 링크를 웹 페이지에 저장 하시겠습니까? 웹 페이지의 소스 코드? 텍스트 전용 콘텐츠? 제목, 저자 및 기사 본문을 분리해야합니까? 이것은 너무 광범위하거나 애매한 질문입니다. 이 문제를 스스로 구현 한 다음 문제가 생길 때 특정 질문을 게시하십시오. –

+1

@ Lèse : Austin은 XML을 구문 분석하려고합니다. 그 후에 그가 그 일을하는 것은 그 분의 책임입니다. –

+1

HTML/XML 파서 (및 XPath) 이외에는 정규식을 사용하지 말아야합니다. –

답변

0

각 사이트마다 구문 분석기를 작성해야합니다. 이게 뭔가 ...

class Parser_Article_SarajevoX extends Parser_Article implements Parser_Interface_Article { 

    protected static $_url = 'http://www.sarajevo-x.com/'; 

    public static function factory($url) 
    { 
     return new Parser_Article_SarajevoX($url); 
    } 

    protected static function decode($string) 
    { 
     return iconv('ISO-8859-2', Kohana::$charset, $string); 
    } 

    /** 
    * SarajevoX Article Parser constructor 
    * 
    * @param string article's url or uri 
    */ 
    public function __construct($url) 
    { 
     $parsed = parse_url($url); 

     if ($path = arr::get($parsed, 'path')) 
     { 
      // make url's and uri's path the same 
      $path = trim($path, '/'); 

      $exploded = explode('/', $path); 

      if (count($exploded == 4)) 
      { 
       list($this->cat_main, $this->cat, $nita, $this->id) = $exploded; 
      } 
      elseif (count($exploded) == 3) 
      { 
       list($this->cat, $nita, $this->id) = $exploded; 
      } 
      else 
      { 
       throw new Exception("Path not recognized: :url", array(':url' => $url)); 
      } 

      // @todo check if this article is already imported to skip getting HTML 

      $html = HTML_Parser::factory(self::$_url.$path); 

      $content = $html->find('#content-main .content-bg', 0); 

      // @freememory 
      $html = NULL; 

      $this->title = self::decode($content->find('h1', 0)->innertext); 

      // Loop through all inner divs and find the content 
      foreach ($content->find('div') as $div) 
      { 
       switch ($div->class) 
       { 
        case 'nadnaslov': 

         $this->suptitle = strip_tags(self::decode($div->innertext)); 

        break; 
        case 'uvod': 

         $this->subtitle = strip_tags(self::decode($div->innertext)); 

        break; 
        case 'tekst': 

         $pic_wrap = $div->find('div[id="fotka"]', 0); 

         if ($pic_wrap != FALSE) 
         { 
          $this->_pictures[] = array 
          (
           'url' => self::$_url.trim($pic_wrap->find('img', 0)->src, '/'), 
           'desc' => self::decode($pic_wrap->find('div[id="opisslike"]', 0)->innertext), 
          ); 

          // @freememory 
          $pic_wrap = NULL; 
         } 

         $this->content = strip_tags(self::decode($div->innertext)); 

        break; 
        case 'ad-gallery' : 

         foreach ($div->find('div[id="gallery"] .ad-nav .ad-thumbs ul li a') as $a) 
         { 
          $this->_pictures[] = array 
          (
           'url' => self::$_url.trim($a->href, '/'), 
           'desc' => self::decode($a->find('img', 0)->alt), 
          ); 

          // @freememory 
          $a = NULL; 
         } 

        break; 
       } 
      } 

      echo Kohana::debug($this); 

      return; 
     } 

     throw new Exception("Path not recognized: :url", array(':url' => $url)); 
    } 

} 
0

RSS 피드는 XML이므로 xml_parse_into_struct과 같은 것을 사용하면이 피드의 구문 분석을 시작할 수 있습니다. 이 페이지의 예제는 당신을 잘 가게 할 수있을만큼 충분해야합니다.

0

각 블로그에는 연관된 rss xml 파일이 있습니다. 블로그 페이지에는 헤더에이 xml 파일을 가리키는 "링크"태그가 있으므로 브라우저에서 사용자가 RSS 피드를 구독 할 수 있습니다. rss xml 파일에는 제목, 설명, 게시 날짜, URL과 같은 모든 블로그 항목에 필요한 모든 데이터가 있습니다. PHP simpleXML 클래스를 사용하여 XML 컨텐트를 simpleXML 객체로로드하려고합니다. 그런 다음 필요한 RSS 피드의 각 조각에 액세스 할 수 있습니다.

관련 문제