php采集远程文章简单类
<?php
/**
* 采集类
* @author Milkcy
* @copyright (C) 2012-2015 TCCMS.COM
* @lastmodify 2012-07-10 14:00
*/
class gather { public $pagestring = '';
private $db; function __construct() {
global $db;
$this->db = $db;
} function geturlfile($url) {
$url = trim($url);
$content = '';
if (extension_loaded('curl')) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);
$content = curl_exec($ch);
curl_close($ch);
} else {
$content = file_get_contents($url);
}
return trim($content);
} function get_all_url($code) {
preg_match_all('/<a.+?href=["|\\']?([^>"\\' ]+)["|\\']?\\s*[^>]*>([^>]+)<\\/a>/is', $code, $arr);
return array('name' => $arr[2], 'url' => $arr[1]);
} function get_sub_content($str, $start, $end) {
$start = trim($start);
$end = trim($end);
if ($start == '' || $end == '') {
return $str;
}
$str = explode($start, $str);
$str = explode($end, $str[1]);
return $str[0];
} function vd($var) {
echo "<div style=\\"border:1px solid #ddd;background:#F7F7F7;padding:5px 10px;\\">\\r\\n";
echo "<pre style=\\"font-family:Arial,Vrinda;font-size:14px;\\">\\r\\n";
var_dump($var);
echo "\\r\\n</pre>\\r\\n";
echo "</div>";
} } ?> <?php
define('ROOT_PATH', str_replace('\\\\', '/', dirname(__FILE__)));
include ROOT_PATH."/gather.class.php";
set_time_limit(0);
header("Content-type: text/html; charset=gb2312");
//目标网址
$url = 'http://news.163.com/special/00013C0O/guojibjtj_03.html';
//实例化采集机器
$gather = new gather();
//获取目标网址HTML
$html = $gather->geturlfile($url);
//定义采集列表区间
$start = '<div class="bd clearfix">';
$end = '<div class="pages-1 mt25">';
//获取区间内的文章URL和TITLE
$code = $gather->get_sub_content($html, $start, $end);
$newsAry = $gather->get_all_url($code);
//打印出结果
//$gather->vd($newsAry);
$tarGetUrl = $newsAry['url'][0];
//获取目标网址HTML
$html = $gather->geturlfile($tarGetUrl);
//定义采集列表区间
$start = '<div id="endText">';
$end = '<span class="cDGray right" style="white-space:nowrap;">';
//获取区间内的文章URL和TITLE
$code = $gather->get_sub_content($html, $start, $end);
$killHtml = '<iframe src="http://g.163.com/r?site=netease&affiliate=news&cat=article&type=tvscreen200x300&location=1" width="200" height="300" frameborder="no" border="0" marginwidth="0" marginheight="0" scrolling="no"></iframe>';
$killHtml2 = '<a href="http://news.163.com/"><img src="http://img1.cache.netease.com/cnews/img07/end_i.gif" alt="netease" width="12" height="11" border="0" class="icon" /></a>';
$code = str_replace($killHtml, "", $code);
$code = str_replace($killHtml2, "", $code);
$gather->vd($code);
?>
//该片段来自于http://outofmemory.cn
php 文章采集正则代码
//采集html
function getwebcontent($url){
$ch = curl_init();
$timeout = 10;
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;
} //获得标题和url
$string =
getwebcontent('http://www.***.com/learn/zhunbeihuaiyun/jijibeiyun/2');
//正则匹配<li>获取标题和地址
preg_match_all ("/<li><a href=\"\/learn\/article\/(.*)\">(.*)<\/a>/",$string, $out, PREG_SET_ORDER);
foreach($out as $key => $value){
$article['title'][] = $out[$key][2];
$article['link'][] = "http://www.***.com/learn/article/".$out[$key][1];
}
//根据url获取文章内容
foreach($article['link'] as $key=>$value){
$content_html = getwebcontent($article['link'][$key]);
preg_match("/<div id=pagenum_0(.*)>[\s|\S]*?<\/div>/",$content_html,$matches);
$article[content][$key] = $matches[0]; }
//不转码还真不能保存成文件
foreach($article[title] as $key=>$value){
$article[title][$key] = iconv('utf-8', 'gbk', $value);//转码
}
//存入文件
$num = count($article['title']);
for($i=0; $i<$num; $i++){
file_put_contents("{$article[title][$i]}.txt", $article['content'][$i]);
}
?>
php采集远程文章简单类的更多相关文章
- ThinkPHP Http工具类(用于远程采集 远程下载) phpSimpleHtmlDom采集类库_Jquery筛选方式 使用phpQuery轻松采集网页内容http://www.thinkphp.cn/extend/541.html
[php]代码库 view sourceprint? <?php // +------------------------------------------------------------ ...
- PHP+jQuery 长文章分页类 ( 支持 url / ajax 分页方式 )
/* ******* 环境:Apache2.2.8 ( 2.2.17 ) + PHP5.2.6 ( 5.3.3 ) + MySQL5.0.51b ( 5.5.8 ) + jQuery-1.8 **** ...
- PHP扩展开发-简单类扩展
今天来学习简单类扩展开发 实现目标为如下php的类 <?php class classext(){ private $username; CONST URL="http://www.g ...
- 带SSL证书的httpclient 远程接口工具类
package com.iups.wx.util; import java.io.IOException; import java.io.UnsupportedEncodingException; i ...
- Spring 远程调用工具类RestTemplateUtils
Spring 远程调用Rest服务工具类,包含Get.Post.Put.Delete四种调用方式. 依赖jar <dependency> <groupId>org.spring ...
- (一)基于阿里云的MQTT远程控制(Android 连接MQTT服务器,ESP8266连接MQTT服务器实现远程通信控制----简单的连接通信)
如果不了解MQTT的可以看这篇文章 http://www.cnblogs.com/yangfengwu/p/7764667.html http://www.cnblogs.com/yangfengw ...
- JVM系列文章(四):类载入机制
作为一个程序猿,只知道怎么用是远远不够的. 起码,你须要知道为什么能够这么用.即我们所谓底层的东西. 那究竟什么是底层呢?我认为这不能一概而论.以我如今的知识水平而言:对于Web开发人员,TCP/IP ...
- Away3d 基础 1 ---对一个简单类的解释
转自:http://www.cnblogs.com/nooon/archive/2009/05/16/1458334.html 原英文地址: http://www.flashmagazine.com/ ...
- PHP扩展开发之简单类开发
接下来我们要用扩展的形式实现以下类(演示环境:linux.php-5.5.34-src) <?php class Person { private $_name; public function ...
随机推荐
- Linux 最常用命令小结
1. 文件共享 1).将windows 系统下的文件夹共享到linux的方法: 安装filezilla,设置连接linux 服务器.将文件上传. 2).mRemote 机器连接管理 2. 文件管理命令 ...
- 第四篇、CocoaPods 镜像的更新 原来的淘宝镜像已经不再更新
在开发应用,我们常常使用cocoaPods来管理第三方框架,但是原来的淘宝的镜像不更新了 新的镜像地址:https://gems.ruby-china.org/
- Cocos2d-x中使用音频CocosDenshion引擎介绍与音频文件的预处理
Cocos2d-x提供了一个音频CocosDenshion引擎,CocosDenshion引擎可以独立于Cocos2d-x单独使用,CocosDenshion引擎本质上封装了OpenAL音频处理库.具 ...
- Java实战之04JavaWeb-03会话技术
一.会话技术简介 1.什么是会话,为什么需要会话技术? 会话:从打开一个浏览器,访问某个网站,到关闭这个浏览器的这个过程称为一次会话.http协议是状态的. 2.会话技术的分类 客户端存储技术:Coo ...
- Android版的疯狂猜图游戏源码完整版分享
这个游戏源码是在安装教程网那么分享过来的,Android版的疯狂猜图游戏源码完整版分享,也是本人之前很早以前发的一款游戏源码的,大家如果想了解一下,可以看看吧,不说多了,上一个图先吧. > ...
- JavaScript正则实战
*:first-child { margin-top: 0 !important; } .markdown-body>*:last-child { margin-bottom: 0 !impor ...
- C#对象转JSON字符串和JSON字符串转对象
namespace Net.String.ConsoleApplication { using System; using System.Data; using System.Collections; ...
- JS中的!=、== 、!==、===的用法和区别。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 var num = 1; var str = '1'; var test = 1; t ...
- 淘宝IP地址库采集器c#代码
这篇文章主要介绍了淘宝IP地址库采集器c#代码,有需要的朋友可以参考一下. 最近做一个项目,功能类似于CNZZ站长统计功能,要求显示Ip所在的省份市区/提供商等信息.网上的Ip纯真数据库,下载下来一看 ...
- php 批量替换html标签的实例代码
php批量替换html标签的实例代码分享. 1.把html元素全部去掉,或者保留某几个html标签 <?php $text = '<p>Test paragraph.</p ...