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))
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/69946337/viewspace-2763870/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- Python 爬蟲 (四) --多執行緒Python爬蟲執行緒
- Python《多執行緒併發爬蟲》Python執行緒爬蟲
- 通過網路圖片小爬蟲對比Python中單執行緒與多線(進)程的效率爬蟲Python執行緒
- Python爬蟲入門【9】:圖蟲網多執行緒爬取Python爬蟲執行緒
- Python 爬蟲 (五) --多執行緒續 (Queue )Python爬蟲執行緒
- 簡易多執行緒爬蟲框架執行緒爬蟲框架
- 多執行緒爬蟲實現(上)執行緒爬蟲
- python爬蟲入門八:多程式/多執行緒Python爬蟲執行緒
- python爬蟲之多執行緒、多程式+程式碼示例Python爬蟲執行緒
- 資料提取方法-多程式多執行緒爬蟲執行緒爬蟲
- Python爬蟲入門【10】:電子書多執行緒爬取Python爬蟲執行緒
- python多執行緒非同步爬蟲-Python非同步爬蟲試驗[Celery,gevent,requests]Python執行緒非同步爬蟲
- Python3多執行緒爬蟲例項講解Python執行緒爬蟲
- 如何使用queue模組實現多執行緒爬蟲執行緒爬蟲
- Python爬蟲:一些常用的爬蟲技巧總結(IP,cookie,header,多執行緒)Python爬蟲CookieHeader執行緒
- 爬蟲筆記:提高資料採集效率!代理池和執行緒池的使用爬蟲筆記執行緒
- ***PHP多執行緒pthreads實現QQ號碼爬蟲PHP執行緒thread爬蟲
- 多執行緒查詢,效率翻倍執行緒
- Python爬蟲入門教程 13-100 鬥圖啦表情包多執行緒爬取Python爬蟲執行緒
- Python爬蟲入門教程 11-100 行行網電子書多執行緒爬取Python爬蟲執行緒
- python進階(15)多執行緒與多程式效率測試Python執行緒
- Python爬蟲和java爬蟲哪個效率高Python爬蟲Java
- 爬蟲養成記--千軍萬馬來相見(詳解多執行緒)爬蟲執行緒
- 多執行緒------執行緒與程式/執行緒排程/建立執行緒執行緒
- python多執行緒爬去糗事百科Python執行緒
- 使用 Python 編寫多執行緒爬蟲抓取百度貼吧郵箱與手機號Python執行緒爬蟲
- 最令人頭疼的Python問題:Python多執行緒在爬蟲中的應用Python執行緒爬蟲
- Muduo 多執行緒模型對比執行緒模型
- Python爬蟲15--爬蟲遇上多執行緒,速度更上一層樓,爬取1000張圖片連一分鐘也不要!Python爬蟲執行緒
- win32多執行緒和C++11多執行緒對比Win32執行緒C++
- 多執行緒爬取B站視訊執行緒
- 以生活例子說明單執行緒與多執行緒執行緒
- 提高爬蟲爬取效率的辦法爬蟲
- 爬蟲代理是如何執行的?爬蟲
- 【多執行緒總結(二)-執行緒安全與執行緒同步】執行緒
- 在scrapy框架下建立爬蟲專案,建立爬蟲檔案,執行爬蟲檔案框架爬蟲
- python豆瓣多執行緒爬蟲加IP代理(免費的一般是不穩定)Python執行緒爬蟲
- python多執行緒程式設計1— python對多執行緒的支援Python執行緒程式設計