1 <?php 2 3 $urls = array( 4 ‘http://www.sina.com.cn/‘, 5 ‘http://www.sohu.com/‘, 6 ‘http://www.163.com/‘ , 7 ‘https://www.baidu.com‘, 8 ‘http://www.sina.com.cn/‘, 9 ‘http://www.sohu.com/‘, 10 ‘http://www.163.com/‘ , 11 ‘https://www.baidu.com‘, 12 ‘http://www.sina.com.cn/‘, 13 ‘http://www.sohu.com/‘, 14 ‘http://www.163.com/‘ , 15 ‘https://www.baidu.com‘, 16 ‘http://www.sina.com.cn/‘, 17 ‘http://www.sohu.com/‘, 18 ‘http://www.163.com/‘ , 19 ‘https://www.baidu.com‘, 20 ‘http://www.sina.com.cn/‘, 21 ‘http://www.sohu.com/‘, 22 ‘http://www.163.com/‘ , 23 ‘https://www.baidu.com‘, 24 ‘http://www.sina.com.cn/‘, 25 ‘http://www.sohu.com/‘, 26 ‘http://www.163.com/‘ , 27 ‘https://www.baidu.com‘, 28 ‘http://www.sina.com.cn/‘, 29 ‘http://www.sohu.com/‘, 30 ‘http://www.163.com/‘ , 31 ‘https://www.baidu.com‘, 32 ‘http://www.sina.com.cn/‘, 33 ‘http://www.sohu.com/‘, 34 ‘http://www.163.com/‘ , 35 ‘https://www.baidu.com‘, 36 ‘http://www.sina.com.cn/‘, 37 ‘http://www.sohu.com/‘, 38 ‘http://www.163.com/‘ , 39 ‘https://www.baidu.com‘, 40 ); // 设置要抓取的页面URL 41 42 $save_to=‘./test.txt‘; // 把抓取的代码写入该文件 43 $st = fopen($save_to,"a"); 44 $conn = array(); 45 $mh = curl_multi_init(); 46 foreach ($urls as $i => $url) { 47 $conn[$i] = curl_init($url); 48 curl_setopt($conn[$i],CURLOPT_USERAGENT,"Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0)"); 49 curl_setopt($conn[$i],CURLOPT_HEADER,0); 50 curl_setopt($conn[$i],CURLOPT_CONNECTTIMEOUT,60); 51 curl_setopt($conn[$i],CURLOPT_FILE,$st); 52 curl_multi_add_handle($mh,$conn[$i]); 53 } 54 55 do{ 56 curl_multi_exec($mh,$active); 57 }while ($active) ; 58 59 foreach ($urls as $i => $url) { 60 curl_multi_remove_handle($mh,$conn[$i]); 61 curl_close($conn[$i]); 62 } 63 64 curl_multi_close($mh); 65 fclose($st);
上边是没有设置多线程的
<?php $urls = array( ‘http://www.sina.com.cn/‘, ‘http://www.sohu.com/‘, ‘http://www.163.com/‘ , ‘https://www.baidu.com‘, ‘http://www.sina.com.cn/‘, ‘http://www.sohu.com/‘, ‘http://www.163.com/‘ , ‘https://www.baidu.com‘, ‘http://www.sina.com.cn/‘, ‘http://www.sohu.com/‘, ‘http://www.163.com/‘ , ‘https://www.baidu.com‘, ‘http://www.sina.com.cn/‘, ‘http://www.sohu.com/‘, ‘http://www.163.com/‘ , ‘https://www.baidu.com‘, ‘http://www.sina.com.cn/‘, ‘http://www.sohu.com/‘, ‘http://www.163.com/‘ , ‘https://www.baidu.com‘, ‘http://www.sina.com.cn/‘, ‘http://www.sohu.com/‘, ‘http://www.163.com/‘ , ‘https://www.baidu.com‘, ‘http://www.sina.com.cn/‘, ‘http://www.sohu.com/‘, ‘http://www.163.com/‘ , ‘https://www.baidu.com‘, ‘http://www.sina.com.cn/‘, ‘http://www.sohu.com/‘, ‘http://www.163.com/‘ , ‘https://www.baidu.com‘, ‘http://www.sina.com.cn/‘, ‘http://www.sohu.com/‘, ‘http://www.163.com/‘ , ‘https://www.baidu.com‘, ); // 设置要抓取的页面URL $conn = array(); $save_to=‘./test2.txt‘; // 把抓取的代码写入该文件 $st = fopen($save_to,‘a‘); foreach ($urls as $i => $url) { $conn[$i] = curl_init($url); curl_setopt($conn[$i],CURLOPT_USERAGENT,"Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0)"); curl_setopt($conn[$i],CURLOPT_HEADER,0); curl_setopt($conn[$i],CURLOPT_CONNECTTIMEOUT,60); curl_setopt($conn[$i],CURLOPT_FILE,$st); curl_exec($conn[$i]); } foreach ($urls as $i => $url) { curl_close($conn[$i]); } fclose($st);
ab 测试差12秒左右
时间: 2024-10-13 06:54:53