基于curl数据采集之单页面并行采集函数get_htmls的使用_PHP
用第一篇的get_html()实现简单的数据采集,由于是一个一个执行才采集数据的传输时间就会是所有页面下载的总时长,一个页面假设1秒,那么10个页面就是10秒了。所幸curl还提供了并行处理的功能。
要写一个并行采集的函数,先要了解要采集什么样的页面,对采集的页面用什么请求,才能写出一个相对常用的函数。
功能需求分析:
返回什么?
当然每一个页面的html集合成的数组
传递什么参数?
编写get_html()时,我们知道了可以用options数组来传递更多的curl参数,那么多页面同时采集函数的编写这种特性也得保留下来。
什么类型的参数?
无论是请求网页HTML,还是调用互联网api接口,get和post传递参数总是请求同一个页面或者接口,只是参数不同罢了。那么参数的类型是:
get_htmls($url,$options);
$url 是string
$options,是一个二维数组,每一个页面的参数为一个数组。
这样的话,貌似解决了问题。但是我找遍了curl的手册都没有看到get的参数传递在什么地方,所以只能$url 是数组的形式传递并且增加一个method参数
函数的原型就定下来了get_htmls($urls,$options = array, $method = ‘get');代码如下:
复制代码 代码如下:
function get_htmls($urls, $options = array(), $method = 'get'){
$mh = curl_multi_init();
if($method == 'get'){//get方式传值 最常用
foreach($urls as $key=>$url){
$ch = curl_init($url);
$options[CURLOPT_RETURNTRANSFER] = true;
$options[CURLOPT_TIMEOUT] = 5;
curl_setopt_array($ch,$options);
$curls[$key] = $ch;
curl_multi_add_handle($mh,$curls[$key]);
}
}elseif($method == 'post'){//post方式传值
foreach($options as $key=>$option){
$ch = curl_init($urls);
$option[CURLOPT_RETURNTRANSFER] = true;
$option[CURLOPT_TIMEOUT] = 5;
$option[CURLOPT_POST] = true;
curl_setopt_array($ch,$option);
$curls[$key] = $ch;
curl_multi_add_handle($mh,$curls[$key]);
}
}else{
exit("参数出错!\n");
}
do{
$mrc = curl_multi_exec($mh,$active);
curl_multi_select($mh);//减少CPU压力 注释掉CPU压力变大
}while($active);
foreach($curls as $key=>$ch){
$html = curl_multi_getcontent($ch);
curl_multi_remove_handle($mh,$ch);
curl_close($ch);
$htmls[$key] = $html;
}
curl_multi_close($mh);
return $htmls;
}
常用的get请求是通过改变url参数来实现的,又因为我们的函数是针对数据采集的。必然是分类采集,所以网址类似于这种:
http://www.baidu.com/s?wd=shili&pn=0&ie=utf-8
http://www.baidu.com/s?wd=shili&pn=10&ie=utf-8
http://www.baidu.com/s?wd=shili&pn=20&ie=utf-8
http://www.baidu.com/s?wd=shili&pn=30&ie=utf-8
http://www.baidu.com/s?wd=shili&pn=50&ie=utf-8
上面五个页面是很有规律的,改变的仅仅是pn的值。
复制代码 代码如下:
$urls = array();
for($i=1; $i $urls[] = 'http://www.baidu.com/s?wd=shili&pn='.(($i-1)*10).'&ie=utf-8';
}
$option[CURLOPT_USERAGENT] = 'Mozilla/5.0 (Windows NT 6.1; rv:19.0) Gecko/20100101 Firefox/19.0';
$htmls = get_htmls($urls,$option);
foreach($htmls as $html){
echo $html;//这里得到html 就可以进行数据处理了
}
模拟常用的post请求:
写一个post.php文件如下:
复制代码 代码如下:
if(isset($_POST['username']) && isset($_POST['password'])){
echo '用户名是: '.$_POST['username'].' 密码是: '.$_POST['password'];
}else{
echo '请求错误!';
}
然后调用如下:
复制代码 代码如下:
$url = 'http://localhost/yourpath/post.php';//这里是你的路径
$options = array();
for($i=1; $i $option[CURLOPT_POSTFIELDS] = 'username=user'.$i.'&password=pass'.$i;
$options[] = $option;
}
$htmls = get_htmls($url,$options,'post');
foreach($htmls as $html){
echo $html;//这里得到html 就可以进行数据处理了
}
这样这个get_htmls函数也基本能实现一些数据采集的功能了
今天分享就到这里 写的不好的 讲得不清楚的 请多多指教

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Alipay PHP...

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

Session hijacking can be achieved through the following steps: 1. Obtain the session ID, 2. Use the session ID, 3. Keep the session active. The methods to prevent session hijacking in PHP include: 1. Use the session_regenerate_id() function to regenerate the session ID, 2. Store session data through the database, 3. Ensure that all session data is transmitted through HTTPS.

How to debug CLI mode in PHPStorm? When developing with PHPStorm, sometimes we need to debug PHP in command line interface (CLI) mode...

The application of SOLID principle in PHP development includes: 1. Single responsibility principle (SRP): Each class is responsible for only one function. 2. Open and close principle (OCP): Changes are achieved through extension rather than modification. 3. Lisch's Substitution Principle (LSP): Subclasses can replace base classes without affecting program accuracy. 4. Interface isolation principle (ISP): Use fine-grained interfaces to avoid dependencies and unused methods. 5. Dependency inversion principle (DIP): High and low-level modules rely on abstraction and are implemented through dependency injection.

How to automatically set the permissions of unixsocket after the system restarts. Every time the system restarts, we need to execute the following command to modify the permissions of unixsocket: sudo...

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

Sending JSON data using PHP's cURL library In PHP development, it is often necessary to interact with external APIs. One of the common ways is to use cURL library to send POST�...
