爬虫性能分析
对于爬虫,python进行并发抓取的实现方式主要有以下几种:进程,线程,协程。 性能的消耗主要在IO请求中,当单进程单线程模式下请求URL时必然会引起等待,从而使得请求整体变慢。 一 多进程执行 可以实现并发,但是,请求发送出去后和返回之前,中间时期进程空闲 编写方式: 1- 多进程直接返回处理 1 from concurrent.futures import ProcessPoolExecutor 2 import requests 3 import time 4 5 def task(url): 6 response = requests.get(url) 7 print(url,response) 8 # 写正则表达式 9 return response 10 11 pool = ProcessPoolExecutor(7) 12 url_list = [ 13 'http://www.cnblogs.com/wupeiqi', 14 'http://huaban.com/favorite/beauty/', 15 'http://www.bing.com', 16 'http://www.zhihu.com', 17 'http://www.sina.com', 18 'http://www.baidu.com', 19 'http://www.autohome.com