脚本之家,脚本语言编程技术及教程分享平台!
分类导航

Python|VBS|Ruby|Lua|perl|VBA|Golang|PowerShell|Erlang|autoit|Dos|bat|

服务器之家 - 脚本之家 - Python - Python爬虫之线程池的使用

Python爬虫之线程池的使用

2021-10-28 08:36世界的隐喻 Python

这篇文章主要介绍了Python爬虫之线程池的使用,文中有非常详细的的代码示例,对正在学习python爬虫的小伙伴们很有帮助哟。需要的朋友可以参考下

一、前言

学到现在,我们可以说已经学习了爬虫的基础知识,如果没有那些奇奇怪怪的反爬虫机制,基本上只要有时间分析,一般的数据都是可以爬取的,那么到了这个时候我们需要考虑的就是爬取的效率了,关于提高爬虫效率,也就是实现异步爬虫,我们可以考虑以下两种方式:一是线程池的使用(也就是实现单进程下的多线程),一是协程的使用(如果没有记错,我所使用的协程模块是从python3.4以后引入的,我写博客时使用的python版本是3.9)。

今天我们先来讲讲线程池。

二、同步代码演示

我们先用普通的同步的形式写一段代码

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
import time
 
def func(url):
    print("正在下载:", url)
    time.sleep(2)
    print("下载完成:", url)
 
if __name__ == '__main__':
    start = time.time() # 开始时间
 
    url_list = [
        "a", "b", "c"
    ]
 
    for url in url_list:
        func(url)
 
    end = time.time() # 结束时间
 
    print(end - start)

对于代码运行的结果我们心里都有数,但还是让我们来看一下吧

Python爬虫之线程池的使用

不出所料。运行时间果然是六秒

三、异步,线程池代码

那么如果我们使用线程池运行上述代码又会怎样呢?

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
import time
from multiprocessing import pool
 
def func(url):
    print("正在下载:", url)
    time.sleep(2)
    print("下载完成:", url)
 
if __name__ == '__main__':
    start = time.time() # 开始时间
 
    url_list = [
        "a", "b", "c"
    ]
 
    pool = pool(len(url_list)) # 实例化一个线程池对象,并且设定线程池的上限数量为列表长度。不设置上限也可以。
 
    pool.map(func, url_list)
 
    end = time.time() # 结束时间
 
    print(end - start)

下面就是见证奇迹的时候了,让我们运行程序

Python爬虫之线程池的使用

我们发现这次我们的运行时间只用2~3秒。其实我们可以将线程池简单的理解为将多个任务同时进行。

注意:

1.我使用的是 pycharm,如果使用的是 vs 或者说是 python 自带的 idle,在运行时我们只能看到最后时间的输出。

2.我们输出结果可能并不是按 abc 的顺序输出的。

四、同步爬虫爬取图片

因为我们的重点是线程池的爬取效率提高,我们就简单的爬取一页的图片。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import requests
import time
import os
from lxml import etree
 
def save_photo(url, title):
    # ua伪装
    header = {
        "user-agent": "mozilla/5.0 (windows nt 10.0; win64; x64) applewebkit/537.36 (khtml, like gecko) chrome/90.0.4430.93 safari/537.36"
    }
 
    # 发送请求
    photo = requests.get(url=url, headers=header).content
 
    # 创建路径,避免重复下载
    if not os.path.exists("c:\\users\\asus\\desktop\\csdn\\高性能异步爬虫\\线程池\\同步爬虫爬取4k美女图片\\" + title + ".jpg"):
        with open("c:\\users\\asus\\desktop\\csdn\\高性能异步爬虫\\线程池\\同步爬虫爬取4k美女图片\\" + title + ".jpg", "wb") as fp:
            print(title, "开始下载!!!")
            fp.write(photo)
            print(title, "下载完成!!!")
 
if __name__ == '__main__':
    start = time.time()
 
    # 创建文件夹
    if not os.path.exists("c:\\users\\asus\\desktop\\csdn\\高性能异步爬虫\\线程池\\同步爬虫爬取4k美女图片"):
        os.mkdir("c:\\users\\asus\\desktop\\csdn\\高性能异步爬虫\\线程池\\同步爬虫爬取4k美女图片")
 
    # ua伪装
    header = {
        "user-agent": "mozilla/5.0 (windows nt 10.0; win64; x64) applewebkit/537.36 (khtml, like gecko) chrome/90.0.4430.93 safari/537.36"
    }
 
    # 指定url
    url = "https://pic.netbian.com/4kmeinv/"
 
    # 发送请求,获取源码
    page = requests.get(url = url, headers = header).text
 
    # xpath 解析,获取图片的下载地址的列表
    tree = etree.html(page)
    url_list = tree.xpath('//*[@id="main"]/div[3]/ul/li/a/@href')
    # 通过下载地址获取高清图片的地址和图片名称
    for href in url_list:
        new_url = "https://pic.netbian.com" + href
        # 再一次发送请求
        page = requests.get(url = new_url, headers = header).text
        # 再一次 xpath 解析
        new_tree = etree.html(page)
        src = "https://pic.netbian.com" + new_tree.xpath('//*[@id="img"]/img/@src')[0]
        title = new_tree.xpath('//*[@id="img"]/img/@title')[0].split(" ")[0]
        # 编译文字
        title = title.encode("iso-8859-1").decode("gbk")
        # 下载,保存
        save_photo(src, title)
 
    end = time.time()
    print(end - start)

让我们看看同步爬虫需要多长时间

Python爬虫之线程池的使用

然后再让我们看看使用线程池的异步爬虫爬取这些图片需要多久

五、使用线程池的异步爬虫爬取4k美女图片

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import requests
import time
import os
from lxml import etree
from multiprocessing import pool
 
def save_photo(src_title):
    # ua伪装
    header = {
        "user-agent": "mozilla/5.0 (windows nt 10.0; win64; x64) applewebkit/537.36 (khtml, like gecko) chrome/90.0.4430.93 safari/537.36"
    }
 
    # 发送请求
    url = src_title[0]
    title = src_title[1]
    photo = requests.get(url=url, headers=header).content
 
    # 创建路径,避免重复下载
    if not os.path.exists("c:\\users\\asus\\desktop\\csdn\\高性能异步爬虫\\线程池\\异步爬虫爬取4k美女图片\\" + title + ".jpg"):
        with open("c:\\users\\asus\\desktop\\csdn\\高性能异步爬虫\\线程池\\异步爬虫爬取4k美女图片\\" + title + ".jpg", "wb") as fp:
            print(title, "开始下载!!!")
            fp.write(photo)
            print(title, "下载完成!!!")
 
if __name__ == '__main__':
    start = time.time()
 
    # 创建文件夹
    if not os.path.exists("c:\\users\\asus\\desktop\\csdn\\高性能异步爬虫\\线程池\\异步爬虫爬取4k美女图片"):
        os.mkdir("c:\\users\\asus\\desktop\\csdn\\高性能异步爬虫\\线程池\\异步爬虫爬取4k美女图片")
 
    # ua伪装
    header = {
        "user-agent": "mozilla/5.0 (windows nt 10.0; win64; x64) applewebkit/537.36 (khtml, like gecko) chrome/90.0.4430.93 safari/537.36"
    }
 
    # 指定url
    url = "https://pic.netbian.com/4kmeinv/"
 
    # 发送请求,获取源码
    page = requests.get(url = url, headers = header).text
 
    # xpath 解析,获取图片的下载地址的列表
    tree = etree.html(page)
    url_list = tree.xpath('//*[@id="main"]/div[3]/ul/li/a/@href')
    # 存储最后的网址和标题的列表
    src_list = []
    title_list = []
    # 通过下载地址获取高清图片的地址和图片名称
    for href in url_list:
        new_url = "https://pic.netbian.com" + href
        # 再一次发送请求
        page = requests.get(url = new_url, headers = header).text
        # 再一次 xpath 解析
        new_tree = etree.html(page)
        src = "https://pic.netbian.com" + new_tree.xpath('//*[@id="img"]/img/@src')[0]
        src_list.append(src)
        title = new_tree.xpath('//*[@id="img"]/img/@title')[0].split(" ")[0]
        # 编译文字
        title = title.encode("iso-8859-1").decode("gbk")
        title_list.append(title)
 
    # 下载,保存。使用线程池
    pool = pool()
    src_title = zip(src_list, title_list)
    pool.map(save_photo, list(src_title))
 
    end = time.time()
    print(end - start)

让我们来看看运行的结果

Python爬虫之线程池的使用

只用了 17 秒,可不要小瞧这几秒,如果数据太大,这些差距后来就会更大了。

注意

不过我们必须要明白 线程池 是有上限的,这就是说数据太大,线程池的效率也会降低,所以这就要用到协程模块了。

到此这篇关于python爬虫之线程池的使用的文章就介绍到这了,更多相关python线程池的使用内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!

原文链接:https://blog.csdn.net/ShiJieDeYinYu/article/details/116608974

延伸 · 阅读

精彩推荐