首頁 php教程 PHP源码 页面抓取程序

页面抓取程序

May 26, 2016 am 08:20 AM

跳至

_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(&#39;#([a-zA-Z]+)#is&#39;, $a[&#39;title&#39;], $match);
			$a_title = strtoupper(implode("", $match[0]));
			
			preg_match_all(&#39;#([a-zA-Z]+)#is&#39;, $b[&#39;title&#39;], $match);
			$b_title = strtoupper(implode("", $match[0]));
			return $a_title > $b_title;
		});*/
		$res = file_put_contents(
			$this->cacheDataDir . &#39;/list.txt&#39;,
			array_map(function($a_list){
				$str = sprintf(
					"标题:%s\t超链接:%s \n",
					str_replace(" ", "", $a_list[&#39;title&#39;]),
					$a_list[&#39;href&#39;]
				);
				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[&#39;content&#39;]);
		$list = $html[$this->request->fetch_item_query];
		foreach ($list as $v) {
			$v = pq($v);

			$item = array(
				"title" => $v->attr(&#39;title&#39;) ? $v->attr(&#39;title&#39;) : $v->text(),
				"href" => real_url($v->attr(&#39;href&#39;), $this->request->base_url)
			);
			$this->article_list[md5($item[&#39;href&#39;])] = $item;
		}
		$page_current = $html[$this->request->fetch_page_current];
		if ($page_current->next()->text()) {
			$page = ++ $param[&#39;page&#39;];
			$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__ . &#39;/&#39;. trim($_POST[&#39;cache_path&#39;], &#39;/&#39;) .&#39;/&#39;;
		$this->fetch_item_query = $_POST[&#39;fetch_item_query&#39;];
		$this->fetch_page_current = $_POST[&#39;fetch_page_current&#39;];
		$this->base_url = $_POST[&#39;url&#39;];
	}
	function request() {
		if (strstr($_POST[&#39;url&#39;], &#39;?&#39;)) {
			$url = sprintf("%s&auth=%s", $_POST[&#39;url&#39;], $auth);
		} else {
			$url = sprintf("%s?auth=%s", $_POST[&#39;url&#39;], $auth);
		}
		$param = array();
		if (isset($_POST[&#39;param&#39;])) {
			foreach($_POST[&#39;param&#39;] as $k => $item) {
				if (!empty($item[&#39;method&#39;]) && !empty($item[&#39;name&#39;])) {
					$param[$item[&#39;method&#39;]][$item[&#39;name&#39;]] = $item[&#39;value&#39;];
				}
			}
		}
		if (isset($param[&#39;get&#39;]) && !empty($param[&#39;get&#39;])) {
			foreach ($param[&#39;get&#39;] as $name => $value) {
				$url = sprintf("%s&%s=%s", $url, $name, $value);
			}
		}
		$post_data = null;
		if (isset($param[&#39;post&#39;]) && !empty($param[&#39;post&#39;])) {
			$post_data = $param[&#39;post&#39;];
		}
	}
}
?>fetch_list();
	
	//$myCurl->fetch_article();
	myDebug::set_end();
} else {
	$_POST[&#39;url&#39;] = &#39;http://www.oschina.net/code/tag/php?show=time&lang=&catalog=&p=%d&#39;;
	$_POST[&#39;cache_path&#39;] = &#39;oschina&#39;;
	$_POST[&#39;fetch_item_query&#39;] = &#39;.code_list ul li .code_title > a&#39;;
	$_POST[&#39;fetch_page_current&#39;] = &#39;.pager li.current&#39;;
}
?>页面爬虫
登入後複製
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

Video Face Swap

Video Face Swap

使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

<🎜>:泡泡膠模擬器無窮大 - 如何獲取和使用皇家鑰匙
3 週前 By 尊渡假赌尊渡假赌尊渡假赌
北端:融合系統,解釋
3 週前 By 尊渡假赌尊渡假赌尊渡假赌

熱工具

記事本++7.3.1

記事本++7.3.1

好用且免費的程式碼編輯器

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

禪工作室 13.0.1

禪工作室 13.0.1

強大的PHP整合開發環境

Dreamweaver CS6

Dreamweaver CS6

視覺化網頁開發工具

SublimeText3 Mac版

SublimeText3 Mac版

神級程式碼編輯軟體(SublimeText3)

熱門話題

Java教學
1664
14
CakePHP 教程
1423
52
Laravel 教程
1318
25
PHP教程
1268
29
C# 教程
1248
24