python爬蟲(chóng)中多線程的使用詳解
queue介紹
queue是python的標(biāo)準(zhǔn)庫(kù),俗稱隊(duì)列.可以直接import引用,在python2.x中,模塊名為Queue。python3直接queue即可
在python中,多個(gè)線程之間的數(shù)據(jù)是共享的,多個(gè)線程進(jìn)行數(shù)據(jù)交換的時(shí)候,不能夠保證數(shù)據(jù)的安全性和一致性,所以當(dāng)多個(gè)線程需要進(jìn)行數(shù)據(jù)交換的時(shí)候,隊(duì)列就出現(xiàn)了,隊(duì)列可以完美解決線程間的數(shù)據(jù)交換,保證線程間數(shù)據(jù)的安全性和一致性。
#多線程實(shí)戰(zhàn)栗子(糗百)
#用一個(gè)隊(duì)列Queue對(duì)象,
#先產(chǎn)生所有url,put進(jìn)隊(duì)列;
#開(kāi)啟多線程,把queue隊(duì)列作為參數(shù)傳入
#主函數(shù)中讀取url
import requests
from queue import Queue
import re,os,threading,time
# 構(gòu)造所有ip地址并添加進(jìn)queue隊(duì)列
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36'
}
urlQueue = Queue()
[urlQueue.put('http://www.qiumeimei.com/image/page/{}'.format(i)) for i in range(1,14)]
def get_image(urlQueue):
while True:
try:
# 不阻塞的讀取隊(duì)列數(shù)據(jù)
url = urlQueue.get_nowait()
# i = urlQueue.qsize()
except Exception as e:
break
print('Current Thread Name %s, Url: %s ' % (threading.currentThread().name, url))
try:
res = requests.get(url, headers=headers)
url_infos = re.findall('data-lazy-src="(.*?)"', res.text, re.S)
for url_info in url_infos:
if os.path.exists(img_path + url_info[-20:]):
print('圖片已存在')
else:
image = requests.get(url_info, headers=headers)
with open(img_path + url_info[-20:], 'wb') as fp:
time.sleep(1)
fp.write(image.content)
print('正在下載:' + url_info)
except Exception as e:
print(e)
if __name__ == '__main__':
startTime = time.time()
# 定義圖片存儲(chǔ)路徑
img_path = './img/'
if not os.path.exists(img_path):
os.mkdir(img_path)
threads = []
# 可以調(diào)節(jié)線程數(shù), 進(jìn)而控制抓取速度
threadNum = 4
for i in range(0, threadNum):
t = threading.Thread(target=get_image, args=(urlQueue,))
threads.append(t)
for t in threads:
t.start()
for t in threads:
# 多線程多join的情況下,依次執(zhí)行各線程的join方法, 這樣可以確保主線程最后退出, 且各個(gè)線程間沒(méi)有阻塞
t.join()
endTime = time.time()
print('Done, Time cost: %s ' % (endTime - startTime))
總結(jié)
以上所述是小編給大家介紹的python爬蟲(chóng)中多線程的使用詳解,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
如果你覺(jué)得本文對(duì)你有幫助,歡迎轉(zhuǎn)載,煩請(qǐng)注明出處,謝謝!
- Python基礎(chǔ)進(jìn)階之海量表情包多線程爬蟲(chóng)功能的實(shí)現(xiàn)
- python3爬蟲(chóng)中多線程進(jìn)行解鎖操作實(shí)例
- python3爬蟲(chóng)GIL修改多線程實(shí)例講解
- python3爬蟲(chóng)中多線程的優(yōu)勢(shì)總結(jié)
- python爬蟲(chóng)開(kāi)發(fā)之使用Python爬蟲(chóng)庫(kù)requests多線程抓取貓眼電影TOP100實(shí)例
- python支持多線程的爬蟲(chóng)實(shí)例
- Python 微信爬蟲(chóng)完整實(shí)例【單線程與多線程】
- python面向?qū)ο蠖嗑€程爬蟲(chóng)爬取搜狐頁(yè)面的實(shí)例代碼
- python爬蟲(chóng)爬取快手視頻多線程下載功能
- Python3多線程爬蟲(chóng)實(shí)例講解代碼
- php與python實(shí)現(xiàn)的線程池多線程爬蟲(chóng)功能示例
- python 多線程爬取壁紙網(wǎng)站的示例
相關(guān)文章
matplotlib subplots 設(shè)置總圖的標(biāo)題方法
今天小編就為大家分享一篇matplotlib subplots 設(shè)置總圖的標(biāo)題方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-05-05
關(guān)于pip安裝opencv-python遇到的問(wèn)題
這篇文章主要介紹了關(guān)于pip安裝opencv-python遇到的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-02-02
Python設(shè)計(jì)模式之模板方法模式實(shí)例詳解
這篇文章主要介紹了Python設(shè)計(jì)模式之模板方法模式,結(jié)合實(shí)例形式較為詳細(xì)的分析了模板方法模式的概念、原理及Python定義、使用模板方法模式相關(guān)操作技巧,需要的朋友可以參考下2019-01-01
使用python的Flask框架進(jìn)行上傳和下載文件詳解

