我有一个简单的问题。我正在尝试使用php构建一个网站,收集来自不同博客的文章。我将如何在PHP中编码?我需要某种类型的正则表达式声明吗?我需要做的就是从特定页面中获取文章。一个例子是:http://rss.news.yahoo.com/rss/education 有人可以帮忙吗?谢谢。
答案 0 :(得分:0)
RSS源是XML,因此您可以使用类似xml_parse_into_struct
的内容来开始解析此Feed。这个页面上的例子应该足以让你前进。
答案 1 :(得分: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));
}
}
答案 2 :(得分:0)
每个博客都有一个关联的rss xml文件。博客页面将在其标题中指向此xml文件的“链接”标记,以便浏览器可以允许用户订阅这些RSS订阅源。 rss xml文件将包含每个博客条目的所有必需数据,例如标题,描述,发布日期,URL。您将需要使用PHP simpleXML类将XML内容加载到simpleXML对象中。然后,您可以访问所需的每个RSS源。