4
A
回答
3
这是比在这里粘贴一些代码更多的参与。但我可以指出你需要做的事情的正确方向。
- 首先,你需要抓取网页
- 解析你回来找RSS Autodiscovery Meta tag的字符串。您可以将整个文档映射为XML并使用DOM遍历,但我只是使用正则表达式。
- 提取标签的href部分,并且您现在拥有RSS提要的URL。
1
的rules for making RSS discoverable被相当详细的记载。您只需要解析HTML并查找所描述的元素。
13
的一般过程已经回答(Quentin,DOOManiac),所以一些代码(Demo):
<?php
$location = 'http://hakre.wordpress.com/';
$html = file_get_contents($location);
echo getRSSLocation($html, $location); # http://hakre.wordpress.com/feed/
/**
* @link http://keithdevens.com/weblog/archive/2002/Jun/03/RSSAuto-DiscoveryPHP
*/
function getRSSLocation($html, $location){
if(!$html or !$location){
return false;
}else{
#search through the HTML, save all <link> tags
# and store each link's attributes in an associative array
preg_match_all('/<link\s+(.*?)\s*\/?>/si', $html, $matches);
$links = $matches[1];
$final_links = array();
$link_count = count($links);
for($n=0; $n<$link_count; $n++){
$attributes = preg_split('/\s+/s', $links[$n]);
foreach($attributes as $attribute){
$att = preg_split('/\s*=\s*/s', $attribute, 2);
if(isset($att[1])){
$att[1] = preg_replace('/([\'"]?)(.*)\1/', '$2', $att[1]);
$final_link[strtolower($att[0])] = $att[1];
}
}
$final_links[$n] = $final_link;
}
#now figure out which one points to the RSS file
for($n=0; $n<$link_count; $n++){
if(strtolower($final_links[$n]['rel']) == 'alternate'){
if(strtolower($final_links[$n]['type']) == 'application/rss+xml'){
$href = $final_links[$n]['href'];
}
if(!$href and strtolower($final_links[$n]['type']) == 'text/xml'){
#kludge to make the first version of this still work
$href = $final_links[$n]['href'];
}
if($href){
if(strstr($href, "http://") !== false){ #if it's absolute
$full_url = $href;
}else{ #otherwise, 'absolutize' it
$url_parts = parse_url($location);
#only made it work for http:// links. Any problem with this?
$full_url = "http://$url_parts[host]";
if(isset($url_parts['port'])){
$full_url .= ":$url_parts[port]";
}
if($href{0} != '/'){ #it's a relative link on the domain
$full_url .= dirname($url_parts['path']);
if(substr($full_url, -1) != '/'){
#if the last character isn't a '/', add it
$full_url .= '/';
}
}
$full_url .= $href;
}
return $full_url;
}
}
}
return false;
}
}
+0
优秀!它对我来说工作得很好 – fortytwo
1
一个稍微小一点的函数,将抓取第一个可用的feed,不管它是rss还是atom(大多数博客有两个选项 - 这抓住了第一选择)。
public function getFeedUrl($url){
if(@file_get_contents($url)){
preg_match_all('/<link\srel\=\"alternate\"\stype\=\"application\/(?:rss|atom)\+xml\"\stitle\=\".*href\=\"(.*)\"\s\/\>/', file_get_contents($url), $matches);
return $matches[1][0];
}
return false;
}
相关问题
- 1. 使用PHP获取RSS提要网址?
- 2. 从rss feed获取图片网址?
- 3. 转换'(')用于RSS网址
- 4. 如何使用PHP将RSS源添加到我的网站中
- 5. 阅读本网站的RSS
- 6. 如何从FeedBurner网址获取图像形式的RSS提要?
- 7. 如何从其他网站获取月明智的rss feed
- 8. 如何从移动类型网站获取旧的RSS提要?
- 9. 使用php和simplexml从rss feed抓取图片网址
- 10. NSXMLParser rss图片网址?
- 11. 验证RSS提要网址
- 12. 如何在html页面中获取RSS网址?
- 13. 如何获取网站的网址?
- 14. 如何通过RSS从其他网站获取新闻
- 15. 网站地图/ RSS/Atom,SEO
- 16. RSS从网站到Facebook Fanpage
- 17. 从Wordpress网站阅读RSS
- 18. 如何为网站创建RSS Feed?
- 19. ASP.NET获取Twitter的RSS网名
- 20. 使用PHP打开以.rss结尾的网址
- 21. 创建一个没有RSS按钮的网站的RSS源
- 22. 从网站上的RSS提供的RSS源
- 23. WordPress的RSS和作者的网址
- 24. 无法在少数网站中找到rss供稿网址
- 25. 获取应用程序名称/网址请求Rss Feed C#
- 26. 从没有rss的站点提取rss
- 27. 自定义VIMEO RSS输出获取/提取网址
- 28. 获取网站上的所有RSS链接
- 29. 获取一个网站的RSS订阅人数
- 30. Facebook的RSS饲料网址变化
嗨,你是否提及有关html源报废以确定RSS饲料网址? – Jeyaganesh