Python3多线程爬虫入门实例

内容摘要
这篇文章主要为大家详细介绍了Python3多线程爬虫入门实例,具有一定的参考价值,可以用来参考一下。

对python这个高级语言对此感兴趣的朋友,看看idc笔记做的技术笔记!
多线程概
文章正文

这篇文章主要为大家详细介绍了Python3多线程爬虫入门实例,具有一定的参考价值,可以用来参考一下。

对python这个高级语言对此感兴趣的朋友,看看idc笔记做的技术笔记!

多线程概述

多线程使得程序内部可以分出多个线程来做多件事情,充分利用CPU空闲时间,提升处理效率。python提供了两个模块来实现多线程thread 和threading ,thread 有一些缺点,在threading 得到了弥补。并且在Python3中废弃了thread模块,保留了更强大的threading模块。

使用场景

在python的原始解释器CPython中存在着GIL(Global Interpreter Lock,全局解释器锁),因此在解释执行python代码时,会产生互斥锁来限制线程对共享资源的访问,直到解释器遇到I/O操作或者操作次数达到一定数目时才会释放GIL。所以,虽然CPython的线程库直接封装了系统的原生线程,但CPython整体作为一个进程,同一时间只会有一个获得GIL的线程在跑,其他线程则处于等待状态。这就造成了即使在多核CPU中,多线程也只是做着分时切换而已。

如果你的程序是CPU密集型,多个线程的代码很有可能是线性执行的。所以这种情况下多线程是鸡肋,效率可能还不如单线程因为有上下文切换开销。但是如果你的代码是IO密集型,涉及到网络、磁盘IO的任务都是IO密集型任务,多线程可以明显提高效率,例如多线程爬虫,多线程文件处理等等

多线程爬虫

多线程爬虫的代码实例

注: 以下代码在python3下运行通过, python2版本差异较大,不能运行成功,如需帮助请下方留意。


# @param Python3多线程爬虫实例讲解
# @author php教程|www.512Pic.com 

# coding=utf-8
import threading, queue, time, urllib
from  urllib import request
baseUrl = 'https://www.idcnote.com/html/pythonjichu/'
urlQueue = queue.Queue()
for i in range(2, 10):
    url = baseUrl + str(i) + '.html'
    urlQueue.put(url)
    #print(url)
def fetchUrl(urlQueue):
    while True:
        try:
            #不阻塞的读取队列数据
            url = urlQueue.get_nowait()
            i = urlQueue.qsize()
        except Exception as e:
            break
        print ('Current Thread Name %s, Url: %s ' % (threading.currentThread().name, url))
        try:
            response = urllib.request.urlopen(url)
            responseCode = response.getcode()
        except Exception as e:
            continue
        if responseCode == 200:
            #抓取内容的数据处理可以放到这里
            #为了突出效果, 设置延时
            time.sleep(1)
if __name__ == '__main__':
    startTime = time.time()
    threads = []
    # 可以调节线程数, 进而控制抓取速度
    threadNum = 4
    for i in range(0, threadNum):
        t = threading.Thread(target=fetchUrl, args=(urlQueue,))
        threads.append(t)
    for t in threads:
        t.start()
    for t in threads:
        #多线程多join的情况下,依次执行各线程的join方法, 这样可以确保主线程最后退出, 且各个线程间没有阻塞
        t.join()
    endTime = time.time()
    print ('Done, Time cost: %s ' %  (endTime - startTime))

# End www_512pic_com

运行结果:

1个线程时:

<style type="text/css">p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px 'Bitstream Vera Sans Mono'; color: #29f914; background-color: #000000} span.s1 {font-variant-ligatures: no-common-ligatures}</style>

# @param Python3多线程爬虫实例讲解
# @author php教程|www.512Pic.com 

Current Thread Name Thread-1, Url: https://www.idcnote.com/html/pythonjichu/2.html 
Current Thread Name Thread-1, Url: https://www.idcnote.com/html/pythonjichu/3.html 
Current Thread Name Thread-1, Url: https://www.idcnote.com/html/pythonjichu/4.html 
Current Thread Name Thread-1, Url: https://www.idcnote.com/html/pythonjichu/5.html 
Current Thread Name Thread-1, Url: https://www.idcnote.com/html/pythonjichu/6.html 
Current Thread Name Thread-1, Url: https://www.idcnote.com/html/pythonjichu/7.html 
Current Thread Name Thread-1, Url: https://www.idcnote.com/html/pythonjichu/8.html 
Current Thread Name Thread-1, Url: https://www.idcnote.com/html/pythonjichu/9.html 
Done, Time cost: 8.182249069213867

# End www_512pic_com

2个线程时:

<style type="text/css">p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px 'Bitstream Vera Sans Mono'; color: #29f914; background-color: #000000} span.s1 {font-variant-ligatures: no-common-ligatures}</style>

# @param Python3多线程爬虫实例讲解
# @author php教程|www.512Pic.com 

Current Thread Name Thread-1, Url: https://www.idcnote.com/html/pythonjichu/2.html 
Current Thread Name Thread-2, Url: https://www.idcnote.com/html/pythonjichu/3.html 
Current Thread Name Thread-2, Url: https://www.idcnote.com/html/pythonjichu/4.html 
Current Thread Name Thread-1, Url: https://www.idcnote.com/html/pythonjichu/5.html 
Current Thread Name Thread-2, Url: https://www.idcnote.com/html/pythonjichu/6.html 
Current Thread Name Thread-1, Url: https://www.idcnote.com/html/pythonjichu/7.html 
Current Thread Name Thread-2, Url: https://www.idcnote.com/html/pythonjichu/8.html 
Current Thread Name Thread-1, Url: https://www.idcnote.com/html/pythonjichu/9.html 
Done, Time cost: 4.0987958908081055

# End www_512pic_com

3个线程时:

<style type="text/css">p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px 'Bitstream Vera Sans Mono'; color: #29f914; background-color: #000000} span.s1 {font-variant-ligatures: no-common-ligatures}</style>

# @param Python3多线程爬虫实例讲解
# @author php教程|www.512Pic.com 

Current Thread Name Thread-1, Url: https://www.idcnote.com/html/pythonjichu/2.html 
Current Thread Name Thread-2, Url: https://www.idcnote.com/html/pythonjichu/3.html 
Current Thread Name Thread-3, Url: https://www.idcnote.com/html/pythonjichu/4.html 
Current Thread Name Thread-4, Url: https://www.idcnote.com/html/pythonjichu/5.html 
Current Thread Name Thread-2, Url: https://www.idcnote.com/html/pythonjichu/6.html 
Current Thread Name Thread-4, Url: https://www.idcnote.com/html/pythonjichu/7.html 
Current Thread Name Thread-1, Url: https://www.idcnote.com/html/pythonjichu/9.html 
Current Thread Name Thread-3, Url: https://www.idcnote.com/html/pythonjichu/8.html 
Done, Time cost: 2.287320137023926

# End www_512pic_com

通过调节线程数可以看到,执行时间会随着线程数的增加而缩短,抓取效率成正比增加。

总结:

Python多线程在IO密集型任务,多线程可以明显提高效率,CPU密集型任务不适合使用多线程处理。

注:关于Python3多线程爬虫入门实例的内容就先介绍到这里,更多相关文章的可以留意

代码注释

作者:喵哥笔记

IDC笔记

学的不仅是技术,更是梦想!