设为首页 加入收藏

TOP

用CI框架写的抓取文章标题、内容、来源图片代码(一)
2014-11-23 23:40:01 来源: 作者: 【 】 浏览:23
Tags:框架 文章 标题 内容 来源 图片 代码

整理了一下,之前的有些乱看不清楚,这是我其中一个抓取页面的代码,做一个备份,方便以后调用,本人能力有限希望大家多给点意见

public function snatch()
{
set_time_limit(0);
$this->benchmark->mark('code_start');
/*获取不同类别的二手车新闻*/
for($i=1;$i<=4;$i++)
{
$url = 'http://news.2sche.cn/list.asp stype='.$i;
$result = $this->curl_snatch($url);

preg_match_all('/\d\/(.* )<\/strong>/', $result, $page_news);
//print_r($page_news);
//echo '


';
/*获取单个类别下所有分页页面的新闻列表*/
for($j=1;$j<=$page_news[1];$j++)
{
if(1 == $j)
{
$url_news = 'http://news.2sche.cn/list.asp stype='.$i;
}
else
{
$url_news = 'http://news.2sche.cn/list.asp page='.$j.'&stype='.$i;
}
$result_news = $this->curl_snatch($url_news);
preg_match_all('/
.* <\/a><\/td>/sim', $result_news, $url_newslist);
//print_r($url_newslist);
/*遍历列表页每个url*/
foreach($url_newslist[1] as $url_newslists)
{
$url_newsinfo = 'http://news.2sche.cn/'.$url_newslists;
$result_newsinfo = $this->curl_snatch($url_newsinfo);
/*获取标题*/
preg_match_all('/

(.* )<\/strong><\/h3>/sim', $result_newsinfo, $title);
//print_r($title[1]);
/*获取来源*/
preg_match_all('/

【来源:(.* ) 】<\/span><\/td>/sim', $result_newsinfo, $source);
//print_r($source[1]);
/*获取内容*/
preg_match_all('/
(.* )<\/td>/sim', $result_newsinfo, $content);
//print_r($content[1][0]);
/*获取内容里的所有图片url*/
//preg_match_all('//sim', $content[1][0], $img);
preg_match_all('//sim', $content[1][0], $img);
//echo 'ddddd';
//print_r($img[1]);
//echo 'dddd
';
//exit;
$picture = '';
foreach($img[1] as $imgs)
{
//echo $imgs;
//echo '
';
if(strpos($imgs, 'http://') === false)
{
continue;
}
$img_source = file_get_contents($imgs);

/*获取单个图片的名称*/

$img_names = trim(strrchr($imgs,'/'), '/');
//print_r($img_name);

//echo $img_names;
//exit;
$picture .= $img_names.':';
file_put_contents("./static/uploads/news/".$img_names, $img_source);

//图片路径替换
$img_path = '/static/uploads/news/'.$img_names;
$content[1][0] = str_replace($imgs, $img_path, $content[1][0]);
}
//print_r($picture) ;
//echo 'hhhh
';
//print_r($content[1][0]);

//echo '
';
$data = array(
'title' => $title[1][0],
'source' => $source[1][0],
'contents' => trim($content[1][0]),
'picture' => $picture,
首页 上一页 1 2 下一页 尾页 1/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇HDOJ 2159 FATE (二维背包) 下一篇克服C语言gets函数缺陷的方法初探

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: