search
Homephp教程php手册基于curl数据采集之正则处理函数get_matches的使用

基于curl数据采集之正则处理函数get_matches的使用

Jun 13, 2016 am 11:53 AM
curlgetusefunctionbased ondeal withdata collectionregularof

根据前两篇的博文:

基于curl数据采集之单页面采集函数get_html的使用

基于curl数据采集之单页面并行采集函数get_htmls的使用

已经可以得到了我们需要的html文件,现在需要处理得到的文件获取到我们需要的采集的数据。

对于html文档的解析,没有像XML那样的解析类,因为HTML文档有很多不成对的标签,很不严格。这个时候就需要采用其他的一些辅助类了,simplehtmldom是一个类似于JQuery方式操作HTML文档的解析类。可以很方便的得到想要的数据,可惜速度慢。这里不是我们这里讨论的重点,我主要使用正则来匹配我所需要的采集的数据,可以很快速的得到我需要采集的信息。

考虑到get_html可以判断返回的数据,但是get_htmls没有办法判断,为了方便调式和调用写了如下两个函数:

复制代码 代码如下:


function get_matches($pattern,$html,$err_msg,$multi=false,$flags=0,$offset=0){
     if(!$multi){
         if(!preg_match($pattern,$html,$matches,$flags,$offset)){
             echo $err_msg."! 错误信息: ".get_preg_err_msg()."\n";
             return false;
         }
     }else{
         if(!preg_match_all($pattern,$html,$matches,$flags,$offset)){
             echo $err_msg."! 错误信息: ".get_preg_err_msg()."\n";
             return false;
         }
     }
     return $matches;
 }
 function get_preg_err_msg(){
     $error_code = preg_last_error();
     switch($error_code){
         case PREG_NO_ERROR :
             $err_msg = 'PREG_NO_ERROR';
             break;
         case PREG_INTERNAL_ERROR:
             $err_msg = 'PREG_INTERNAL_ERROR';
             break;
         case PREG_BACKTRACK_LIMIT_ERROR:
             $err_msg = 'PREG_BACKTRACK_LIMIT_ERROR';
             break;
         case PREG_RECURSION_LIMIT_ERROR:
             $err_msg = 'PREG_RECURSION_LIMIT_ERROR';
             break;
         case PREG_BAD_UTF8_ERROR:
             $err_msg = 'PREG_BAD_UTF8_ERROR';
             break;
         case PREG_BAD_UTF8_OFFSET_ERROR:
             $err_msg = 'PREG_BAD_UTF8_OFFSET_ERROR';
             break;
         default:
             return '未知错误!';
     }
     return $err_msg.': '.$error_code;
 }


可以这样调用:

复制代码 代码如下:


$url = 'http://www.baidu.com';
 $html = get_html($url);
 $matches = get_matches('!!',$html,'没有找到链接',true);
 if($matches){
     var_dump($matches);
 }


或者这样调用:

复制代码 代码如下:


$urls = array('http://www.baidu.com','http://www.hao123.com');
 $htmls = get_htmls($urls);
 foreach($htmls as $html){
     $matches = get_matches('!!',$html,'没有找到链接',true);
     if($matches){
         var_dump($matches);
     }
 }


就可以得到所需的信息,无论单页面采集还是多页面采集,最终PHP还是只能处理一个页面,由于使用get_matches了,可以对返回的值进行判断真假,得到正确的数据,由于使用正则的时候遇到了超过正则回溯的问题,增加get_preg_err_msg来提示正则信息。

由于采集数据的时候,经常是采集列表页,根据列表页得到的内容页链接再采集内容页,或者更多的层次,那么循环嵌套会很多,对于代码的控制会感觉力不从心。那我们是否可以把采集列表页的代码和采集内容页的代码,或者更多的层次的采集代码分离开,甚至循环都简化呢?

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.