_init_request($request);
$this->_init_curl();
}
protected function _init_var() {
$this->pageCount = 0;
}
protected function _init_request(request $request) {
$this->request = $request;
// $this->request->cache_path = __DIR__ . '/sjm_cache/';
// $this->request->fetch_item_query = '#J_posts_list .subject .title a';
// $this->request->fetch_page_current = '.J_page_wrap .pages strong';
// $this->request->base_url = 'http://bbs.sijiaomao.com/index.php?m=bbs&c=thread&fid=10&page=%d';
}
protected function _init_curl() {
$this->curl = new CurlMulti();
$this->cacheDir = $this->request->cache_path . 'cache';
if (! is_dir($this->cacheDir)) {
mkdir($this->cacheDir, 777, true);
}
$this->cacheDataDir = $this->request->cache_path . 'data';
if (! is_dir($this->cacheDataDir)) {
mkdir($this->cacheDataDir, 777, true);
}
$this->curl->cache = array(
'dir' => $this->cacheDir,
'on' => true,
'expire' => 3600 * 24
);
$this->curl->maxThread = 10;
$this->curl->opt[CURLOPT_CONNECTTIMEOUT] = 10;
}
public function fetch_list(){
$this->_add_fetch_list_url();
$this->curl->start();
$this->_save_article_list();
}
public function fetch_article() {
foreach ($this->article_list as $k => $v) {
$this->curl->add(array(
'url' => $v['href']
), array($this, '_success_article'));
}
$this->curl->start();
}
public function display() {
printf(
"\n共抓取%d个页面\n文章列表%d篇\n相关文章%d篇\n文章目录存放在%s\n",
$this->pageCount + $this->articleCount,
$this->pageCount,
count($this->article_list),
$this->cacheDataDir . '/list.php'
);
}
public function fetch() {
return sprintf(
"\n共抓取%d个页面\n文章列表%d篇\n相关文章%d篇\n文章目录存放在%s\n",
$this->pageCount + $this->articleCount,
$this->pageCount,
count($this->article_list),
$this->cacheDataDir . '/list.php'
);
}
public function _add_fetch_list_url($page = 1){
$this->curl->add(
array(
'url' => sprintf($this->request->base_url, $page),
'args' => array('page' => $page)
),
array($this, '_success_list')
);
}
protected function _save_article_list() {
$res = file_put_contents(
$this->cacheDataDir . '/list.php',
sprintf("<?php\n return\t%s;",
var_export($this->article_list, true))
);
// 相关性排序整理
/*uasort($this->article_list, function ($a, $b){
preg_match_all('#([a-zA-Z]+)#is', $a['title'], $match);
$a_title = strtoupper(implode("", $match[0]));
preg_match_all('#([a-zA-Z]+)#is', $b['title'], $match);
$b_title = strtoupper(implode("", $match[0]));
return $a_title > $b_title;
});*/
$res = file_put_contents(
$this->cacheDataDir . '/list.txt',
array_map(function($a_list){
$str = sprintf(
"标题:%s\t超链接:%s \n",
str_replace(" ", "", $a_list['title']),
$a_list['href']
);
return $str;
}, $this->article_list)
);
return $res;
}
public function _success_article($r, $param){
++$this->articleCount;
}
public function _success_list($r, $param){
++$this->pageCount;
$html = phpQuery::newDocumentHTML($r['content']);
$list = $html[$this->request->fetch_item_query];
foreach ($list as $v) {
$v = pq($v);
$item = array(
"title" => $v->attr('title') ? $v->attr('title') : $v->text(),
"href" => real_url($v->attr('href'), $this->request->base_url)
);
$this->article_list[md5($item['href'])] = $item;
}
$page_current = $html[$this->request->fetch_page_current];
if ($page_current->next()->text()) {
$page = ++ $param['page'];
$this->_add_fetch_list_url($page);
}
phpQuery::unloadDocuments();
}
}
class request{
/*url*/
public $base_url;
/*缓存文件路径*/
public $cache_path;
/*获取元素的CSS选择器*/
public $fetch_item_query;
/*分页当前页面元素的CSS选择器*/
public $fetch_page_current;
static $instance;
static public function getInstance() {
if (empty(self::$instance)) {
self::$instance = new self;
}
return self::$instance;
}
private function __construct() {
$this->_init_base();
}
function _init_base() {
$this->cache_path = __DIR__ . '/'. trim($_POST['cache_path'], '/') .'/';
$this->fetch_item_query = $_POST['fetch_item_query'];
$this->fetch_page_current = $_POST['fetch_page_current'];
$this->base_url = $_POST['url'];
}
function request() {
if (strstr($_POST['url'], '?')) {
$url = sprintf("%s&auth=%s", $_POST['url'], $auth);
} else {
$url = sprintf("%s?auth=%s", $_POST['url'], $auth);
}
$param = array();
if (isset($_POST['param'])) {
foreach($_POST['param'] as $k => $item) {
if (!empty($item['method']) && !empty($item['name'])) {
$param[$item['method']][$item['name']] = $item['value'];
}
}
}
if (isset($param['get']) && !empty($param['get'])) {
foreach ($param['get'] as $name => $value) {
$url = sprintf("%s&%s=%s", $url, $name, $value);
}
}
$post_data = null;
if (isset($param['post']) && !empty($param['post'])) {
$post_data = $param['post'];
}
}
}
?>fetch_list();
//$myCurl->fetch_article();
myDebug::set_end();
} else {
$_POST['url'] = 'http://www.oschina.net/code/tag/php?show=time&lang=&catalog=&p=%d';
$_POST['cache_path'] = 'oschina';
$_POST['fetch_item_query'] = '.code_list ul li .code_title > a';
$_POST['fetch_page_current'] = '.pager li.current';
}
?>页面爬虫 0
0
本站声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门AI工具
相关专题
本专题系统整理pixiv网页版官网入口及登录访问方式,涵盖官网登录页面直达路径、在线阅读入口及快速进入方法说明,帮助用户高效找到pixiv官方网站,实现便捷、安全的网页端浏览与账号登录体验。
616
2026.02.13
本专题系统整理微博网页版官方入口及网页端登录方式,涵盖首页直达地址、账号登录流程与常见访问问题说明,帮助用户快速找到微博官网主页,实现便捷、安全的网页端登录与内容浏览体验。
194
2026.02.13
本专题围绕Flutter框架展开,系统讲解跨平台UI构建原理与状态管理方案。内容涵盖Widget生命周期、路由管理、Provider与Bloc状态管理模式、网络请求封装及性能优化技巧。通过实战项目演示,帮助开发者构建流畅、可维护的跨平台移动应用。
91
2026.02.13
本专题面向前端开发者,深入讲解 TypeScript 类型系统与大型项目结构设计方法,并结合 Vite 构建工具优化前端工程化流程。内容包括模块化设计、类型声明管理、代码分割、热更新原理以及构建性能调优。通过完整项目示例,帮助开发者提升代码可维护性与开发效率。
20
2026.02.13
本专题围绕 Redis 在高并发系统中的应用展开,系统讲解主从复制、哨兵机制、Cluster 集群模式及数据分片原理。内容涵盖缓存穿透与雪崩解决方案、分布式锁实现、热点数据优化及持久化策略。通过真实业务场景演示,帮助开发者构建高可用、可扩展的分布式缓存系统。
54
2026.02.13
本专题系统整理雨课堂网页版官方入口及在线登录方式,涵盖账号登录流程、官方直连入口及平台访问方法说明,帮助师生用户快速进入雨课堂在线教学平台,实现便捷、高效的课程学习与教学管理体验。
15
2026.02.12
本专题汇总豆包AI官方网页版入口及在线使用方式,涵盖智能写作工具、图片生成体验入口和官网登录方法,帮助用户快速直达豆包AI平台,高效完成文本创作与AI生图任务,实现便捷智能创作体验。
598
2026.02.12
本专题面向后端开发与数据库工程师,深入讲解 PostgreSQL 查询优化原理与索引机制。内容包括执行计划分析、常见索引类型对比、慢查询优化策略、事务隔离级别以及高并发场景下的性能调优技巧。通过实战案例解析,帮助开发者提升数据库响应速度与系统稳定性。
56
2026.02.12
热门下载
精品课程


