у кого есть скрипт для парсинга новостей от Yandex

Статус
В этой теме нельзя размещать новые ответы.

kuzichev

Местный житель
Регистрация
23 Апр 2008
Сообщения
159
Реакции
23
у кого есть скрипт на PHP для парсинга новостей от Yandex
 
а какой смысл яшу парсить? Есть куча нормальных новостных сайтов
 
А разве яша не дает свои новости на других сайтах размещать?
У них же есть что то типо информеров. И тогда спрашивается **х парсить?
 
Спарсит Яшину рсс не проблема, но так как яша сам новости тырит с других сайтов то в принципе лучше сделать несколько парсеров для разных лент ну а если всетаки надо яшу вот это вставляем в самый верх страницы
Код:
<?php
$itemNum=0;
class RSSParser	{
	var $channel_title="";
	var $channel_website="";
	var $channel_description="";
	var $channel_pubDate="";
	var $channel_lastUpdated="";
	var $channel_copyright="";
	var $title="";
	var $link="";
	var $description="";
	var $pubDate="";
	var $author="";
	var $url="";
	var $width="";
	var $height="";
	var $inside_tag=false;	
	function RSSParser($file,$encType)	{
			$this->xml_parser = xml_parser_create($encType);
			xml_set_object( $this->xml_parser, &$this );
			xml_set_element_handler( $this->xml_parser, "startElement", "endElement" );
			xml_set_character_data_handler( $this->xml_parser, "characterData" );
			$fp = @fopen("$file","r") or die( "$file could not be opened" );
			while ($data = fread($fp, 4096)){xml_parse( $this->xml_parser, $data, feof($fp)) or die( "XML error");}
			fclose($fp);
			xml_parser_free( $this->xml_parser );
		}
	
	function startElement($parser,$tag,$attributes=''){
		$this->current_tag=$tag;
		if($this->current_tag=="ITEM" || $this->current_tag=="IMAGE"){
			$this->inside_tag=true;
			$this->description="";
			$this->link="";
			$this->title="";
			$this->pubDate="";
		}
	}
	
	function endElement($parser, $tag){
		switch($tag){
			case "ITEM":
				$this->titles[]=trim($this->title);
				$this->links[]=trim($this->link);
				$this->descriptions[]=trim($this->description);
				$this->pubDates[]=trim($this->pubDate);
				$this->authors[]=trim($this->author);
				$this->author=""; $this->inside_tag=false;
				break;
			case "IMAGE":
				$this->channel_image="<img src=\"".trim($this->url)."\" width=\"".trim($this->width)."\" height=\"".trim($this->height)."\" alt=\"".trim($this->title)."\" border=\"0\" title=\"".trim($this->title)."\" />";
				$this->title=""; $this->inside_tag=false;
			default:
				break;
		}
	}
	
	function characterData($parser,$data){
		if($this->inside_tag){
			switch($this->current_tag){
				case "TITLE":
					$this->title.=$data; break;
				case "DESCRIPTION":
					$this->description.=$data; break;
				case "LINK":
					$this->link.=$data; break;
				case "URL":
					$this->url.=$data; break;					
				case "WIDTH":
					$this->width.=$data; break;
				case "HEIGHT":
					$this->height.=$data; break;
				case "PUBDATE":
					$this->pubDate.=$data; break;
				case "AUTHOR":
					$this->author.=$data;	break;
				default: break;									
			}//end switch
		}else{
			switch($this->current_tag){
				case "DESCRIPTION":
					$this->channel_description.=$data; break;
				case "TITLE":
					$this->channel_title.=$data; break;
				case "LINK":
					$this->channel_website.=$data; break;
				case "COPYRIGHT":
					$this->channel_copyright.=$data; break;
				case "PUBDATE":
					$this->channel_pubDate.=$data; break;					
				case "LASTBUILDDATE":
					$this->channel_lastUpdated.=$data; break;				
				default:
					break;
			}
		}
	}
}

$forex1 = new RSSParser("http://news.yandex.ru/Kostroma/index.rss","utf-8");
?>
дальше вот это вставляем в нужное вам место страницы
Код:
<table width="100%" border="0" class="tab">
          <?php
$forex1_RSSmax=5;
if($forex1_RSSmax==0 || $forex1_RSSmax>count($forex1->titles))$forex1_RSSmax=count($forex1->titles);
for($itemNum=0;$itemNum<$forex1_RSSmax;$itemNum++){?><tr>
            <td align="left" bgcolor="#000000" ><b  style="font-size:12px"><?php 
		$title=$forex1->titles[$itemNum];
		echo  iconv("UTF-8","windows-1251", $title )
		?></b></td>
          </tr>        
          <tr>
            <td align="left" ><?php 
		$descr=$forex1->descriptions[$itemNum];
		echo  iconv("UTF-8","windows-1251", $descr )
		 ?>
            <div class="file"><noindex><a href="<?php echo $forex1->links[$itemNum]; ?>" rel=nofollow>Читать целиком</a></noindex></div></td>
          </tr>
          <?php } ?>
        </table>
_http://news.yandex.ru/Kostroma/index.rss меняете на свой, а да данный пример для страниц на win-1251 если utf ничего конвертить не надо
 
Статус
В этой теме нельзя размещать новые ответы.
Назад
Сверху