Python教程

python并发编程实战(四):使用多线程,python爬虫被加速10倍

本文主要是介绍python并发编程实战(四):使用多线程,python爬虫被加速10倍,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

python创建多线程的方法

单线程、多线程爬取博客园速度对比

tmp/blog_spider.py

import requests

urls = [
    f"https://www.cnblogs.com/#p{page}"
    for page in range(1, 50+1)
]

def craw(url):
    r = requests.get(url)
    print(url, len(r.text))


craw(urls[0])

01.multi_thread_craw.py

import blog_spider
import threading, time


def single_thread():
    print("single_thread begin")
    for url in blog_spider.urls:
        blog_spider.craw(url)
    print("single_thread end")


def multi_thread():
    print("single_thread begin")
    threads = []
    for url in blog_spider.urls:
        threads.append(
            threading.Thread(target=blog_spider.craw, args=(url, ))
        )


    for thread in threads:
        thread.start()

    for thread in threads:
        thread.join()

    print("single_thread end")



if __name__ == '__main__':
    start = time.time()
    single_thread()
    end = time.time()
    print("single_thread cost: ", end - start, "seconds")

    start = time.time()
    multi_thread()
    end = time.time()
    print("multi_thread cost: ", end - start, "seconds")

爬取速度对比
可以看到,速度足足提高了8倍,当然和电脑的性能也有关系
单线程

多线程

这篇关于python并发编程实战(四):使用多线程,python爬虫被加速10倍的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!