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

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

Jun 13, 2016 am 11:53 AM
curl get use function based on deal with data collection regular of

根据前两篇的博文:

基于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 of this Website
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

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1663
14
PHP Tutorial
1263
29
C# Tutorial
1237
24
BTCC tutorial: How to bind and use MetaMask wallet on BTCC exchange? BTCC tutorial: How to bind and use MetaMask wallet on BTCC exchange? Apr 26, 2024 am 09:40 AM

MetaMask (also called Little Fox Wallet in Chinese) is a free and well-received encryption wallet software. Currently, BTCC supports binding to the MetaMask wallet. After binding, you can use the MetaMask wallet to quickly log in, store value, buy coins, etc., and you can also get 20 USDT trial bonus for the first time binding. In the BTCCMetaMask wallet tutorial, we will introduce in detail how to register and use MetaMask, and how to bind and use the Little Fox wallet in BTCC. What is MetaMask wallet? With over 30 million users, MetaMask Little Fox Wallet is one of the most popular cryptocurrency wallets today. It is free to use and can be installed on the network as an extension

Tips for dynamically creating new functions in golang functions Tips for dynamically creating new functions in golang functions Apr 25, 2024 pm 02:39 PM

Go language provides two dynamic function creation technologies: closure and reflection. closures allow access to variables within the closure scope, and reflection can create new functions using the FuncOf function. These technologies are useful in customizing HTTP routers, implementing highly customizable systems, and building pluggable components.

Considerations for parameter order in C++ function naming Considerations for parameter order in C++ function naming Apr 24, 2024 pm 04:21 PM

In C++ function naming, it is crucial to consider parameter order to improve readability, reduce errors, and facilitate refactoring. Common parameter order conventions include: action-object, object-action, semantic meaning, and standard library compliance. The optimal order depends on the purpose of the function, parameter types, potential confusion, and language conventions.

How to match multiple words or strings using Golang regular expression? How to match multiple words or strings using Golang regular expression? May 31, 2024 am 10:32 AM

Golang regular expressions use the pipe character | to match multiple words or strings, separating each option as a logical OR expression. For example: matches "fox" or "dog": fox|dog matches "quick", "brown" or "lazy": (quick|brown|lazy) matches "Go", "Python" or "Java": Go|Python |Java matches words or 4-digit zip codes: ([a-zA

Complete collection of excel function formulas Complete collection of excel function formulas May 07, 2024 pm 12:04 PM

1. The SUM function is used to sum the numbers in a column or a group of cells, for example: =SUM(A1:J10). 2. The AVERAGE function is used to calculate the average of the numbers in a column or a group of cells, for example: =AVERAGE(A1:A10). 3. COUNT function, used to count the number of numbers or text in a column or a group of cells, for example: =COUNT(A1:A10) 4. IF function, used to make logical judgments based on specified conditions and return the corresponding result.

Comparison of the advantages and disadvantages of C++ function default parameters and variable parameters Comparison of the advantages and disadvantages of C++ function default parameters and variable parameters Apr 21, 2024 am 10:21 AM

The advantages of default parameters in C++ functions include simplifying calls, enhancing readability, and avoiding errors. The disadvantages are limited flexibility and naming restrictions. Advantages of variadic parameters include unlimited flexibility and dynamic binding. Disadvantages include greater complexity, implicit type conversions, and difficulty in debugging.

What are the benefits of C++ functions returning reference types? What are the benefits of C++ functions returning reference types? Apr 20, 2024 pm 09:12 PM

The benefits of functions returning reference types in C++ include: Performance improvements: Passing by reference avoids object copying, thus saving memory and time. Direct modification: The caller can directly modify the returned reference object without reassigning it. Code simplicity: Passing by reference simplifies the code and requires no additional assignment operations.

How to write efficient and maintainable functions in Java? How to write efficient and maintainable functions in Java? Apr 24, 2024 am 11:33 AM

The key to writing efficient and maintainable Java functions is: keep it simple. Use meaningful naming. Handle special situations. Use appropriate visibility.

See all articles