[原创]为找房方便 ,将sohu焦点的 rss 改成全文的.

mu_rain 2012-07-02 10:50:33
房价貌似快要涨了,俺最近天天加班,俺媳妇来负责找房源,可她那狗屎公司无法上网,只好用ipad 加载rss 离线看,结果狗屎sohu焦点的rss 非全文rss ,为了老婆找房方便,写了点代码,用于生成全文的rss. 希望借此机会,尝试一下代码改善生活的感觉. 不废话吧,直接贴代码了.

<?php
header("Content-type: text/html; charset=utf-8");
ini_set("max_excution_time" ,3600);
$url = "http://sz.focus.cn/common/xml/rss/news/hot.php";

$newItemArr = parseItems( $url );
$xmlItemContent = makeRssItemString( $newItemArr ); //重新生成item 内容,将下载内容替换descript.

//rss xml 的头尾字符串
$xmlPre = '
<?xml version="1.0" encoding="GBK"?>
<?xml-stylesheet type="text/xsl" href="/common/xml/xsl/rss.xsl" media="all"?>
<?xml-stylesheet type="text/css" href="/common/xml/css/rss.css" media="all"?>

<rss version="2.0"
xmlns:focus="http://house.focus.cn"
xmlns:F="http://house.focus.cn"
docType="rss"
F:docType="rss"
>
<channel
pageIndex=""
pageSize="20"
recordCount=""
F:pageIndex=""
F:pageSize="20"
F:recordCount="">
<image>
<title><![CDATA[热点新闻-搜狐焦点网深圳站 ]]></title>
<link>http://sz.focus.cn/newscenter/xwsy.html</link>
<url>http://images.house.focus.cn/img/newhouselogo.gif</url>
</image>
<title>热点新闻-搜狐焦点网深圳站</title>
<link>http://sz.focus.cn/newscenter/xwsy.html</link>
<description><![CDATA[提供最全面最及时的中国房地产新闻资讯 ]]></description>
<copyright>Copyright 2012, sohu.com Inc., all rights reserved</copyright>
<language>zh-cn</language>
<lastBuildDate>Mon, 02 Jul 2012 21:44:21 +0800</lastBuildDate>
<pubDate>Mon, 02 Jul 2012 21:44:21 +0800</pubDate>
<category>地产综合</category>
<ttl>30</ttl>';
$xmlAppend = '</channel></rss>';


$xmlOutput = $xmlPre . $xmlItemContent . $xmlAppend ;
echo $xmlOutput;


/**
* @param $url
* @return array itemObj
*/
function parseItems( $url )
{
$xmlObj = simplexml_load_file( $url );
$items = $xmlObj -> channel -> item;

$resultArr = array();

foreach( $items as $item ):
$resultArr []= makeRssItemObj( $item );
endforeach;

return $resultArr;
}



/**
* @param $item
* @return itemObj
*/
function makeRssItemObj( $item )
{
// $item->link = "demo.html";
$urlContent = fileGetContent( $item->link ); //如果不支持curl 就用file_get_content 或者socket 连接来实现.

$urlContent = @iconv("gb2312","utf-8",$urlContent);

// $content = iconv("gb2312" , "utf-8" , $content);
preg_match("/id=\"newscontent\">([\d\D]+)<div\s+class=\"clear\">/iU" , $urlContent , $matchArr);

$item->description = $matchArr[1];
return $item;
}

/**
* @param $itemObjArr
* @return string
*/
function makeRssItemString( $itemObjArr )
{
$result = "";

foreach( $itemObjArr as $itemObj ):
$result .= "<item>";
$result .= "<title><![CDATA[ $itemObj->title ]]></title>";
$result .= "<link>$itemObj->link</link>";
$result .= "<description><![CDATA[ $itemObj->description ]]></description>";
$result .= "<author>mu_rain</author>";
$result .= "<pubDate>$itemObj->pubDate</pubDate>";
$result .= "<item>";
endforeach;
return $result;
}


// ------------------------------------------------------------------------
/**
* regulary show the string or object or json.
* 规格化显示
*
* @param $str 对象的实例
* @package P
* @subpackage String
* @category Putils
* @author mu_rain
* @return mixed
*/
// ------------------------------------------------------------------------
function pr($array,$title = 'DEBUG',$type = 'array' , $width = '') {
$title .= date("Y-m-d H:i:s");
$widthStr = "";
if( $width) $widthStr = "width:$width"."px";

echo "<fieldset style=\"-moz-border-radius:5px 5px 5px 5px; -moz-box-shadow:0px 0px 10px rgba(00,00,00,0.45); border: 3px solid transparent; padding:3px; margin-top:20px; \"><legend style=\"color: #069; margin:3px; $widthStr \">$title</legend>";
echo "<div style = '-moz-border-radius:10px 10px 10px 10px;font-size:14px; color:#069; border:1px solid #F0FAF9; font-size:9pt; background:#F0FAF9; padding:5px;'>";
print("<pre>");
if($type == 'json') {
$array = json_decode($array);
}
print_r($array);
print("</pre>");
echo "<div>";
echo "</fieldset>";
}

// ------------------------------------------------------------------------
/**
*
* get the urlContent use curl
* @package KDG
* @subpackage common
* @category mu_rain
* @author 徐兴
*/
// ------------------------------------------------------------------------
function fileGetContent($url)
{
$ch = curl_init();
$timeout = 30;
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
$contents = trim(curl_exec($ch));
curl_close($ch);
return $contents;
}

...全文
498 18 打赏 收藏 转发到动态 举报
写回复
用AI写文章
18 条回复
切换为时间正序
请发表友善的回复…
发表回复
mumubangditu 2012-12-18
  • 打赏
  • 举报
回复
引用 楼主 mu_rain 的回复:
本帖最后由 PhpNewnew 于 2012-07-03 07:50:15 编辑 房价貌似快要涨了,俺最近天天加班,俺媳妇来负责找房源,可她那狗屎公司无法上网,只好用ipad 加载rss 离线看,结果狗屎sohu焦点的rss 非全文rss ,为了老婆找房方便,写了点代码,用于生成全文的rss. 希望借此机会,尝试一下代码改善生活的感觉. 不……
RSS改全文?核心不就是 preg_match("/id=\"newscontent\">([\d\D]+)<div\s+class=\"clear\">/iU" 太简陋了……人家 http://fivefilters.org/content-only/ 是万能的,神马RSS地址贴上去,都能导出成全文滴。
不喝水的鱼 2012-07-04
  • 打赏
  • 举报
回复
出现空行有可能是你的编码是utf-8+bom的
还有就是你在一个地方输出了空行或者\r\n之类的
mu_rain 2012-07-03
  • 打赏
  • 举报
回复
是啊,一步到位好难,想先整个小俩房过渡。 现在每个月房租3k 多,交得心痛,所以就想,直接买个,再贴点钱月供算了。
[Quote=引用 6 楼 的回复:]
我猜老婆是弄房地产的吧..
[/Quote]
mu_rain 2012-07-03
  • 打赏
  • 举报
回复
呵呵。
[Quote=引用 11 楼 的回复:]

惊天大撞秒!!!
#9和#10的回复时间一秒不差!!!
[/Quote]
发张图玩玩.
处理前

处理后

还有个问题,我用php 输出时,第一行为变成空行。 哪怕,代码只有 <?php echo "123"; 也会出现之前空一行。 长久没搞这些,就用了个土办法,直接生成xml 再设置一个crontab 处理了,还请知道的朋友提点一下。
  • 打赏
  • 举报
回复
惊天大撞秒!!!
#9和#10的回复时间一秒不差!!!
  • 打赏
  • 举报
回复
得先看看curl函数了……

房子好遥远……
mu_rain 2012-07-03
  • 打赏
  • 举报
回复
微调一下

//1
function makeRssItemString( $itemObjArr )
{
$result = "";

foreach( $itemObjArr as $itemObj ):
$result .= "<item>";
$result .= "<title><![CDATA[ $itemObj->title ]]></title>";
$result .= "<link>$itemObj->link</link>";
$result .= "<description><![CDATA[ $itemObj->description ]]></description>";
$result .= "<author>mu_rain</author>";
$result .= "<pubDate>$itemObj->pubDate</pubDate>";
$result .= "</item>"; //忘记反 / 了。
endforeach;
return $result;
}

//2
$xmlPre = '
<?xml version="1.0" encoding="GBK"?>
//变更为如下,去除多余的头部空格输出.
$xmlPre = '<?xml version="1.0" encoding="GBK"?>

mu_rain 2012-07-03
  • 打赏
  • 举报
回复
老婆做ic 行业的,仅找房需要~
[Quote=引用 6 楼 的回复:]
我猜老婆是弄房地产的吧..
[/Quote]
黄袍披身 2012-07-03
  • 打赏
  • 举报
回复
我猜老婆是弄房地产的吧..
xuzuning 2012-07-03
  • 打赏
  • 举报
回复
呵呵,准备当房奴了
shelloworlds 2012-07-03
  • 打赏
  • 举报
回复
呵呵,代码改变生活 谢谢
qianshuei 2012-07-02
  • 打赏
  • 举报
回复
准备买房了啊。。。有钱人
inthi 2012-07-02
  • 打赏
  • 举报
回复
这也行、。。。
mu_rain 2012-07-02
  • 打赏
  • 举报
回复
新鲜的代码,自己抢沙发纪念.

21,887

社区成员

发帖
与我相关
我的任务
社区描述
从PHP安装配置,PHP入门,PHP基础到PHP应用
社区管理员
  • 基础编程社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧