给CSDN极客和开发者头条增加RSS源
CSDN极客,网址:http://geek.csdn.net/,经常有一些比较好的链接,对于程序员来说,有一些阅读学习价值。不知从何时开始该网站不再提供RSS订阅,这对于RSS重度使用者来说有点难以忍受。因此花了一下午时间做了一个抓取该网站头条主页内容生成RSS订阅地址的实现。使用PHP后台抓取网页内容,对内容进行正则匹配后过滤出有价值的链接,生成RSS XML格式文档后返回。订阅地址为:http://codefine.site/rss_factory?url=geek.csdn.net。同样,也给支持开发者头条(https://toutiao.io)新增了RSS源:http://codefine.site/rss_factory?url=toutiao.io。
订阅效果:
抓取网页和生成RSS文档的代码如下:
<?php /** * Created by Codefine.site. * User: Shentar * Date: 2018/1/6 * Time: 22:06 */ parse_str($_SERVER['QUERY_STRING'], $queryString); if (empty($queryString)) { echo '404 not found.'; return; } $url = $queryString['url']; if ($url != 'geek.csdn.net' && $url != 'toutiao.io') { echo '404 not supported the url now.'; return; } if ($url == 'geek.csdn.net') { $url = 'http://' . $url; $content = getContent($url); $pattern = '/<span >\n?.*?<a href="(.*)" class="title" target="_blank">(.*)\n?.*?<\/a>/'; if (!empty($content)) { $matches = array(); if (preg_match_all($pattern, $content, $matches)) { if (count($matches) === 3) { $array_link = $matches[1]; $array_text = $matches[2]; if (is_array($array_text) && is_array($array_link) && count($array_link) === count($array_text)) { $body = '<?xml version="1.0"?>' . '<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">' . '<channel>' . '<title>CSDN极客</title>' . '<link>http://geek.csdn.net</link>' . '<atom:link href="http://geek.csdn.net" rel="self" type="application/rss+xml" />' . '<description>CSDN极客</description>' . '<language>zh-CN</language>' . '<generator>码范|CodeFine</generator>'; for ($i = 0; $i < count($array_link); $i++) { $item = '<item>' . '<link>' . '<![CDATA[' . $array_link[$i] . ']]>' . '</link>' . '<title>' . '<![CDATA[' . $array_text[$i] . ']]>' . '</title>' . '</item>'; $body .= $item; } $body .= '</channel></rss>'; // RSS header('Content-Type: application/xml; charset=utf-8'); echo $body; } } } } } if ($url == 'toutiao.io') { $url = 'https://' . $url; $content = getContent($url); $pattern = '/<div class="content" data-url=".*">\n?.*<h3 class="title">\n?.*?<a target="_blank" rel="external" title=".*" href="(.*)">(.*)<\/a>/'; if (!empty($content)) { $matches = array(); if (preg_match_all($pattern, $content, $matches)) { if (count($matches) === 3) { $array_link = $matches[1]; $array_text = $matches[2]; if (is_array($array_text) && is_array($array_link) && count($array_link) === count($array_text)) { $body = '<?xml version="1.0"?>' . '<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">' . '<channel>' . '<title>Top-DEV-News</title>' . '<link>https://toutiao.io</link>' . '<atom:link href="https://toutiao.io" rel="self" type="application/rss+xml" />' . '<description>Top-DEV-News</description>' . '<language>zh-CN</language>' . '<generator>码范|CodeFine</generator>'; for ($i = 0; $i < count($array_link); $i++) { $item = '<item>' . '<link>' . '<![CDATA[' . 'https://toutiao.io' . $array_link[$i] . ']]>' . '</link>' . '<title>' . '<![CDATA[' . $array_text[$i] . ']]>' . '</title>' . '</item>'; $body .= $item; } $body .= '</channel></rss>'; // RSS header('Content-Type: application/xml; charset=utf-8'); echo $body; } } } } } function getContent($url) { $http = new WP_Http(); $res = $http->get($url); $bodyStr = $res['body']; if (!empty($bodyStr)) { return $bodyStr; } else { return ''; } }
2 条评论
你好,
这个订阅地址不发访问了:http://codefine.site/rss_factory?url=geek.csdn.net
好像geek.csdn不再提供服务了,网页的格式变了。