一.概述
对于python爬虫,默认的是使用单线程爬虫,在批量爬数据时速度太慢。但有的人会说,python的多线程爬虫,并不是真正意义上的多线程,它会有阻塞。但是,无论怎么说,多线程或者是多进程,速度都要比原来的单线程要快很多。当你使用多线程爬虫时,电脑风扇都会呜呜的转起来。下面举例说明多线程/多进程的创建方法,以及各种方法运行代码的速度。
二.举例
1.默认情况运行代码
input
1 | import time |
output
1 | Hello a |
2.多线程池的创建与使用
(1)使用ThreadPoolExecutor模块创建
input
1 | import time |
output
1 | Hello a |
(2)使用threading模块创建
input
1 | import time |
output
1 | Hello a |
3.多进程池的创建与使用
input
1 | import time |
output
1 | Hello a |
三.爬虫实例(爬取某网站330张图片)
1.默认


2.使用ThreadPoolExecutor模块


3.使用multiprocessing模块


4.使用threading模块

