当前位置:Gxlcms > PHP教程 > 自己写的一个php基于phpQuery的通用采集类

自己写的一个php基于phpQuery的通用采集类

时间:2021-07-01 10:21:17 帮助过:4人阅读

还是小菜,第一次分享代码哈,这是自己以前写的一个php的采集类,自己一直在用,自我感觉很简单很强大,只要懂一点点选择器的知识就可以采集任何页面了,也支持https页面,做简单的采集足够用了。
  1. /**
  2. *通用列表采集类
  3. *版本V1.3
  4. *作者:JAE
  5. *博客:http://blog.jaekj.com
  6. */
  7. require_once '../phpQuery/phpQuery/phpQuery.php';
  8. class QueryList{
  9. private $pageURL;
  10. private $regArr = array();
  11. public $jsonArr = array();
  12. private $regRange;
  13. private $html;
  14. /************************************************
  15. * 参数: 页面地址 选择器数组 块选择器
  16. * 【选择器数组】说明:格式array("名称"=>array("选择器","类型"),.......)
  17. * 【类型】说明:值 "text" ,"html" ,"属性"
  18. *【块选择器】:指 先按照规则 选出 几个大块 ,然后再分别再在块里面 进行相关的选择
  19. *************************************************/
  20. function QueryList($pageURL,$regArr=array(),$regRange='')
  21. {
  22. $this->pageURL = $pageURL;
  23. //为了能获取https://
  24. $ch = curl_init();
  25. curl_setopt($ch, CURLOPT_URL,$this->pageURL);
  26. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  27. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
  28. curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
  29. $this->html = curl_exec($ch);
  30. curl_close($ch);
  31. if(!empty($regArr))
  32. {
  33. $this->regArr = $regArr;
  34. $this->regRange = $regRange;
  35. $this->getList();
  36. }
  37. }
  38. function setQuery($regArr,$regRange='')
  39. {
  40. $this->jsonArr=array();
  41. $this->regArr = $regArr;
  42. $this->regRange = $regRange;
  43. $this->getList();
  44. }
  45. private function getList()
  46. {
  47. $hobj = phpQuery::newDocumentHTML($this->html);
  48. if(!empty($this->regRange))
  49. {
  50. $robj = pq($hobj)->find($this->regRange);
  51. $i=0;
  52. foreach($robj as $item)
  53. {
  54. while(list($key,$reg_value)=each($this->regArr))
  55. {
  56. $iobj = pq($item)->find($reg_value[0]);
  57. switch($reg_value[1])
  58. {
  59. case 'text':
  60. $this->jsonArr[$i][$key] = trim(pq($iobj)->text());
  61. break;
  62. case 'html':
  63. $this->jsonArr[$i][$key] = trim(pq($iobj)->html());
  64. break;
  65. default:
  66. $this->jsonArr[$i][$key] = pq($iobj)->attr($reg_value[1]);
  67. break;
  68. }
  69. }
  70. //重置数组指针
  71. reset($this->regArr);
  72. $i++;
  73. }
  74. }
  75. else
  76. {
  77. while(list($key,$reg_value)=each($this->regArr))
  78. {
  79. $lobj = pq($hobj)->find($reg_value[0]);
  80. $i=0;
  81. foreach($lobj as $item)
  82. {
  83. switch($reg_value[1])
  84. {
  85. case 'text':
  86. $this->jsonArr[$i++][$key] = trim(pq($item)->text());
  87. break;
  88. case 'html':
  89. $this->jsonArr[$i++][$key] = trim(pq($item)->html());
  90. break;
  91. default:
  92. $this->jsonArr[$i++][$key] = pq($item)->attr($reg_value[1]);
  93. break;
  94. }
  95. }
  96. }
  97. }
  98. }
  99. function getJSON()
  100. {
  101. return json_encode($this->jsonArr);
  102. }
  103. }
  1. require 'Query/QueryList.class.php';
  2. //采集OSC的代码分享列表,标题 链接 作者
  3. $url = "http://www.oschina.net/code/list";
  4. $reg = array("title"=>array(".code_title a:eq(0)","text"),"url"=>array(".code_title a:eq(0)","href"),"author"=>array("img","title"));
  5. $rang = ".code_list li";
  6. $hj = new QueryList($url,$reg,$rang);
  7. $arr = $hj->jsonArr;
  8. print_r($arr);
  9. //如果还想采当前页面右边的 TOP40活跃贡献者 图像,得到JSON数据,可以这样写
  10. $reg = array("portrait"=>array(".hot_top img","src"));
  11. $hj->setQuery($reg);
  12. $json = $hj->getJSON();
  13. echo $json . "
    ";
  14. //采OSC内容页内容
  15. $url = "http://www.oschina.net/code/snippet_186288_23816";
  16. $reg = array("title"=>array(".QTitle h1","text"),"con"=>array(".Content","html"));
  17. $hj = new QueryList($url,$reg);
  18. $arr = $hj->jsonArr;
  19. print_r($arr);
  20. //就举这么多例子吧,是不是用来做采集很方便
  1. /**
  2. *自己写的百度和谷歌搜索API
  3. *版本V2.0
  4. *作者:JAE
  5. *博客:http://blog.jaekj.com
  6. **/
  7. require_once 'QueryList_class.php';
  8. class Searcher
  9. {
  10. private $searcher;
  11. private $key;
  12. private $num;
  13. private $page;
  14. private $regArr ;
  15. private $regRange ;
  16. private $regZnum;
  17. public $jsonArr;
  18. //参数 搜索引擎 搜索关键字 返回的结果条数 第几页
  19. function Searcher($searcher,$key,$num,$page)
  20. {
  21. if($searcher=='baidu')
  22. {
  23. $this->regArr = array("title"=>array("h3.t a,#ting_singlesong_box a","text"),"tCon"=>array("div.c-abstract,font:slice(0,2),div#weibo,table tr:eq(0),div.c-abstract-size p:eq(0),div.vd_sitcom_new_tinfo","text"),"url"=>array("h3.t a,#ting_singlesong_box a","href"));
  24. $this->regRange = 'table.result,table.result-op';
  25. $this->regZnum=array("zNum"=>array("span.nums","text"));
  26. }
  27. else if($searcher=='google')
  28. {
  29. $this->regArr = array("title"=>array("h3.r a","text"),"tCon"=>array("span.st","text"),"url"=>array("h3.r a","href"));
  30. $this->regRange = 'li.g';
  31. $this->regZnum=array("zNum"=>array("div#resultStats","text"));
  32. }
  33. $this->searcher = $searcher;
  34. $this->key = $key;
  35. $this->num = $num;
  36. $this->page = $page-1;
  37. $this->getList();
  38. }
  39. private function getList()
  40. {
  41. $s = urlencode($this->key);
  42. $num = $this->num;
  43. $start = $this->num*$this->page;
  44. if($this->searcher=='baidu')
  45. {
  46. $url = "http://www.baidu.com/s?pn=$start&rn=$num&wd=$s";
  47. $reg_znum='/[\d,]+/';
  48. }
  49. else if($this->searcher=='google')
  50. {
  51. $url="https://www.google.com.hk/search?filter=0&lr=&newwindow=1&safe=images&hl=en&as_qdr=all&num=$num&start=$start&q=$s";
  52. $reg_znum='/([\d,]+) result(s)?/';
  53. }
  54. $searcherObj = new QueryList($url,$this->regArr,$this->regRange);
  55. for($i=0;$ijsonArr);$i++)
  56. {
  57. if($this->searcher=='baidu')
  58. {
  59. $searcherObj->jsonArr[$i]['url'] = $this->getBaiduRealURL($searcherObj->jsonArr[$i]['url']);
  60. }
  61. else if($this->searcher=='google')
  62. {
  63. $searcherObj->jsonArr[$i]['url'] = $this->getGoogleRealURL($searcherObj->jsonArr[$i]['url']);
  64. }
  65. }
  66. $this->jsonArr = $searcherObj->jsonArr ;
  67. //获取总共结果条数
  68. $searcherObj->setQuery($this->regZnum);
  69. $zNum = $searcherObj->jsonArr[0]['zNum'];
  70. preg_match($reg_znum,$zNum,$arr)?$zNum=$arr[0]:$zNum=0;
  71. $zNum = (int)str_replace(',','',$zNum);
  72. //计算总页数
  73. $zPage = ceil($zNum/$this->num);
  74. $this->jsonArr=array('num'=>$this->num,'page'=>((int)$this->page+1),'zNum'=>$zNum,'zPage'=>$zPage,"s"=>"$this->key",'other'=>array('author'=>'JAE','QQ'=>'734708094','blog'=>'http://blog.jaekj.com'),'data'=>$this->jsonArr);
  75. }
  76. function getJSON()
  77. {
  78. return json_encode($this->jsonArr);
  79. }
  80. private function getBaiduRealURL($url)
  81. {
  82. //得到百度跳转的真正地址
  83. $header = get_headers($url,1);
  84. if (strpos($header[0],'301') || strpos($header[0],'302'))
  85. {
  86. if(is_array($header['Location']))
  87. {
  88. //return $header['Location'][count($header['Location'])-1];
  89. return $header['Location'][0];
  90. }
  91. else
  92. {
  93. return $header['Location'];
  94. }
  95. }
  96. else
  97. {
  98. return $url;
  99. }
  100. }
  101. private function getGoogleRealURL($url)
  102. {
  103. $reg_url = '/q=(.+)&/U';
  104. return preg_match($reg_url,$url,$arr)?urldecode($arr[1]):$url;
  105. }
  106. }
  107. // $hj = new Searcher('google','oschina',20,2);
  108. // print_r( $hj->jsonArr);
  109. //效果演示地址
  110. //http://blog.jaekj.com//jae/demo/searcher/Searcher_class.php?searcher=baidu&s=jaekj&num=20&page=1

人气教程排行