用第一篇的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函数也基本能实现一些数据采集的功能了
今天分享就到这里 写的不好的 讲得不清楚的 请多多指教

What’s still popular is the ease of use, flexibility and a strong ecosystem. 1) Ease of use and simple syntax make it the first choice for beginners. 2) Closely integrated with web development, excellent interaction with HTTP requests and database. 3) The huge ecosystem provides a wealth of tools and libraries. 4) Active community and open source nature adapts them to new needs and technology trends.

PHP and Python are both high-level programming languages that are widely used in web development, data processing and automation tasks. 1.PHP is often used to build dynamic websites and content management systems, while Python is often used to build web frameworks and data science. 2.PHP uses echo to output content, Python uses print. 3. Both support object-oriented programming, but the syntax and keywords are different. 4. PHP supports weak type conversion, while Python is more stringent. 5. PHP performance optimization includes using OPcache and asynchronous programming, while Python uses cProfile and asynchronous programming.

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

PHP remains important in the modernization process because it supports a large number of websites and applications and adapts to development needs through frameworks. 1.PHP7 improves performance and introduces new features. 2. Modern frameworks such as Laravel, Symfony and CodeIgniter simplify development and improve code quality. 3. Performance optimization and best practices further improve application efficiency.

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values and handle functions that may return null values.


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

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

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

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

Dreamweaver Mac version
Visual web development tools

WebStorm Mac version
Useful JavaScript development tools

Zend Studio 13.0.1
Powerful PHP integrated development environment