python多线程爬虫与单线程爬虫效率效率对比

import requests

from my_test import settings

import sys

import time

import pymysql

import threading

# 继承父类 threading.Thread

class DownLoadPictures(threading.Thread):

    def __init__(self, name, sn):

        super().__init__()

        self.name = name

        self.headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 '

                                      '(KHTML, like Gecko) Chrome/88.0.4324.150 Safari/537.36',

                        'Referer': 'https://image.so.com/z?ch=beauty'}

        self.url = 'https://image.so.com/zjl?ch=beauty&sn={}'.format(sn)

 

        self.conn = pymysql.Connect(**settings.MYSQL_CONFIG)

        self.cursor = self.conn.cursor()

    def __del__(self):

        self.cursor.close()

        self.conn.close()

    def get_resp_data(self):

        # print(' 当前是链接为 {} 的图片下载! '.format(self.url))

        print(' 当前是线程为 {} 的图片下载! '.format(self.name))

        # 返回的数据在 json

        resp = requests.get(self.url, headers=self.headers)

        return resp.json()

    def run(self):

        # 重写 run 函数,线程在创建后会直接运行 run 函数

        resp_data = self.get_resp_data()

        # 判断是否还有图片

        if resp_data['end'] is False:

            for elem in resp_data['list']:

                downloadurl = elem['qhimg_downurl']

                fromUrl = elem['purl']

                title = elem['title']

                self.download_picture(downloadurl, title, fromUrl)

        else:

            print(' 链接为 {}外汇跟单gendan5.com 已无图片 '.format(self.url))

    def download_picture(self, downloadurl, title, fromUrl):

        sql = "select * from beautyImages where downloadUrl = '{}' and title='{}'".format(downloadurl, title)

        row_count = self.cursor.execute(sql)

        if not row_count:

            try:

                resp = requests.get(downloadurl, headers=self.headers)

                if resp.status_code == requests.codes.ok:

                    with open(settings.STORE_PATH + '/' + title + '.jpg', 'wb') as f:

                        f.write(resp.content)

                print(' 下载完成 ')

                # 插入数据库

                insert_sql = "INSERT INTO beautyImages(title, downloadUrl, fromUrl, createTime) values (%s, %s, %s, %s)"

                try:

                    self.cursor.execute(insert_sql, (title, downloadurl, fromUrl, time.strftime('%Y-%m-%d %H:%M:%S', time.localtime())))

                    self.conn.commit()

                    print(' 插入标题为 {}, 链接为 {} 成功 !'.format(title, downloadurl))

                except Exception:

                    print(' 插入标题为 {}, 链接为 {} 失败 , 失败原因是 {}'.format(title, downloadurl, sys.exc_info()[1]))

            except Exception:

                print(' 标题为 {} , 链接为 {} 下载失败 , 失败原因是 {}'.format(title, downloadurl, sys.exc_info()[1]))

        else:

            print(' 标题为 {} , 链接为 {} 已存在 '.format(title, downloadurl))

if __name__ == '__main__':

    start_time = time.time()

    thread_list = []

    for i in range(0, 301, 30):

        test = DownLoadPictures(name=str(i), sn=i)

        thread_list.append(test)

    for t in thread_list:

        t.start()

    for t in thread_list:

        t.join()

    use_time = time.time() - start_time

    print(' 多线程用时: {} '.format(use_time))


请使用浏览器的分享功能分享到微信等