规范命名

This commit is contained in:
JAE 2014-04-30 11:31:04 +08:00
parent 8bc6607acd
commit 3b7078a1ac

View File

@ -1,5 +1,5 @@
<?php <?php
/** /**
* QueryList * QueryList
* *
* 一个基于phpQuery的通用列表采集类 * 一个基于phpQuery的通用列表采集类
@ -7,89 +7,71 @@
* @author Jaeger * @author Jaeger
* @email 734708094@qq.com * @email 734708094@qq.com
* @link http://git.oschina.net/jae/QueryList * @link http://git.oschina.net/jae/QueryList
* @version 1.6.0 * @version 1.6.1
*/ */
require('phpQuery/phpQuery.php'); require 'phpQuery/phpQuery.php';
class QueryList{ class QueryList
{
private $pageURL; private $pageURL;
private $regArr = array(); private $regArr;
public $jsonArr = array(); public $jsonArr;
private $regRange; private $regRange;
private $html; private $html;
private $output_encoding; private $outputEncoding;
private $html_encoding; private $htmlEncoding;
/** /**
* 构造函数 * 构造函数
* @param string $page 要抓取的网页URL地址(支持https);或者是html源代码 * @param string $page 要抓取的网页URL地址(支持https);或者是html源代码
* @param array $regArr 【选择器数组】说明格式array("名称"=>array("选择器","类型"),.......),【类型】说明:值 "text" ,"html" ,"属性" * @param array $regArr 【选择器数组】说明格式array("名称"=>array("选择器","类型"),.......),【类型】说明:值 "text" ,"html" ,"属性"
* @param string $regRange 【块选择器】:指 先按照规则 选出 几个大块 ,然后再分别再在块里面 进行相关的选择 * @param string $regRange 【块选择器】:指 先按照规则 选出 几个大块 ,然后再分别再在块里面 进行相关的选择
* @param string $getHtmlWay 【源码获取方式】指是通过curl抓取源码还是通过file_get_contents抓取源码 * @param string $getHtmlWay 【源码获取方式】指是通过curl抓取源码还是通过file_get_contents抓取源码
* @param string $output_encoding【输出编码格式】指要以什么编码输出(UTF-8,GB2312,.....),防止出现乱码,如果设置为 假值 则不改变原字符串编码 * @param string $outputEncoding【输出编码格式】指要以什么编码输出(UTF-8,GB2312,.....),防止出现乱码,如果设置为 假值 则不改变原字符串编码
*/ */
public function QueryList($page,$regArr,$regRange='',$getHtmlWay="curl",$output_encoding=false) public function __construct($page, $regArr, $regRange = '', $getHtmlWay = 'curl', $outputEncoding = false)
{
$this->output_encoding = $output_encoding;
if($this->isURL($page))
{ {
$this->outputEncoding = $outputEncoding;
if ($this->_isURL($page)) {
$this->pageURL = $page; $this->pageURL = $page;
if($getHtmlWay=="curl") if ($getHtmlWay == 'curl') {
{
//为了能获取https:// //为了能获取https://
$ch = curl_init(); $ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$this->pageURL); curl_setopt($ch, CURLOPT_URL, $this->pageURL);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$this->html = curl_exec($ch); $this->html = curl_exec($ch);
curl_close($ch); curl_close($ch);
}else{ } else {
$this->html=file_get_contents($this->pageURL); $this->html = file_get_contents($this->pageURL);
} }
}else{ } else {
$this->html = $page; $this->html = $page;
} }
//获取编码格式 //获取编码格式
$this->html_encoding = $this->get_encode($this->html); $this->htmlEncoding = $this->_getEncode($this->html);
if (!empty($regArr)) {
if(!empty($regArr))
{
$this->regArr = $regArr; $this->regArr = $regArr;
$this->regRange = $regRange; $this->regRange = $regRange;
$this->getList(); $this->_getList();
} }
} }
public function setQuery($regArr,$regRange='') public function setQuery($regArr, $regRange = '')
{ {
$this->jsonArr=array(); $this->jsonArr = array();
$this->regArr = $regArr; $this->regArr = $regArr;
$this->regRange = $regRange; $this->regRange = $regRange;
$this->getList(); $this->_getList();
} }
private function getList() private function _getList()
{ {
$hobj = phpQuery::newDocumentHTML($this->html); $hobj = phpQuery::newDocumentHTML($this->html);
if (!empty($this->regRange)) {
if(!empty($this->regRange))
{
$robj = pq($hobj)->find($this->regRange); $robj = pq($hobj)->find($this->regRange);
$i = 0;
$i=0; foreach ($robj as $item) {
foreach($robj as $item) while (list($key, $reg_value) = each($this->regArr)) {
{
while(list($key,$reg_value)=each($this->regArr))
{
$iobj = pq($item)->find($reg_value[0]); $iobj = pq($item)->find($reg_value[0]);
switch ($reg_value[1]) {
switch($reg_value[1])
{
case 'text': case 'text':
$this->jsonArr[$i][$key] = trim(pq($iobj)->text()); $this->jsonArr[$i][$key] = trim(pq($iobj)->text());
break; break;
@ -99,26 +81,18 @@
default: default:
$this->jsonArr[$i][$key] = pq($iobj)->attr($reg_value[1]); $this->jsonArr[$i][$key] = pq($iobj)->attr($reg_value[1]);
break; break;
} }
} }
//重置数组指针 //重置数组指针
reset($this->regArr); reset($this->regArr);
$i++; $i++;
} }
} } else {
else while (list($key, $reg_value) = each($this->regArr)) {
{
while(list($key,$reg_value)=each($this->regArr))
{
$lobj = pq($hobj)->find($reg_value[0]); $lobj = pq($hobj)->find($reg_value[0]);
$i = 0;
foreach ($lobj as $item) {
$i=0; switch ($reg_value[1]) {
foreach($lobj as $item)
{
switch($reg_value[1])
{
case 'text': case 'text':
$this->jsonArr[$i++][$key] = trim(pq($item)->text()); $this->jsonArr[$i++][$key] = trim(pq($item)->text());
break; break;
@ -128,19 +102,13 @@
default: default:
$this->jsonArr[$i++][$key] = pq($item)->attr($reg_value[1]); $this->jsonArr[$i++][$key] = pq($item)->attr($reg_value[1]);
break; break;
}
}
} }
} }
if($this->output_encoding) }
{ }
if ($this->outputEncoding) {
//编码转换 //编码转换
$this->jsonArr = $this->array_convert_encoding($this->jsonArr,$this->output_encoding,$this->html_encoding); $this->jsonArr = $this->_arrayConvertEncoding($this->jsonArr, $this->outputEncoding, $this->htmlEncoding);
} }
} }
public function getJSON() public function getJSON()
@ -152,24 +120,27 @@
* @param $string * @param $string
* @return string * @return string
*/ */
private function get_encode($string){ private function _getEncode($string)
return mb_detect_encoding($string, array('ASCII','GB2312','GBK','UTF-8')); {
return mb_detect_encoding($string, array('ASCII', 'GB2312', 'GBK', 'UTF-8'));
} }
/** /**
* 递归转换数组值得编码格式 * 递归转换数组值得编码格式
* @param array $arr * @param array $arr
* @param string $to_encoding * @param string $toEncoding
* @param string $from_encoding * @param string $fromEncoding
* @return array * @return array
*/ */
private function array_convert_encoding($arr,$to_encoding,$from_encoding) private function _arrayConvertEncoding($arr, $toEncoding, $fromEncoding)
{ {
if(!is_array($arr))return $arr; if (!is_array($arr)) {
return $arr;
}
foreach ($arr as $key => $value) { foreach ($arr as $key => $value) {
if (is_array($value)) { if (is_array($value)) {
$arr[$key] = $this->array_convert_encoding($value,$to_encoding,$from_encoding); $arr[$key] = $this->_arrayConvertEncoding($value, $toEncoding, $fromEncoding);
}else{ } else {
$arr[$key] = mb_convert_encoding($value, $to_encoding,$from_encoding); $arr[$key] = mb_convert_encoding($value, $toEncoding, $fromEncoding);
} }
} }
return $arr; return $arr;
@ -179,13 +150,11 @@
* @param string $str * @param string $str
* @return boolean * @return boolean
*/ */
private function isURL($str) private function _isURL($str)
{
if(preg_match('/^http(s)?:\/\/.+/', $str))
{ {
if (preg_match('/^http(s)?:\\/\\/.+/', $str)) {
return true; return true;
} }
return false; return false;
} }
} }