class Generant(threading.Thread):
def run(self):
while len(page_links_list) > 0:
gLock.acquire() #上锁
page_url = page_links_list.pop()
gLock.release() #释放锁
r = requests.get(page_url,headers = headers)
r.raise_for_status()
r.encoding = r.apparent_encoding
a = re.findall('<img src="https:(.*?)" alt',r.text)
gLock.acquire() #上锁
for i in a :
x = 'https:' + i
x = x.replace('w_487', 'w_1421').replace('h_274', 'h_799')
img_links_list.append(x)
gLock.release() #释放锁
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
消费者代码如下
class Consumer(threading.Thread,):
def run(self):
while True:
gLock.acquire()
if len(img_links_list) == 0:
gLock.release()
continue
else:
img_url = img_links_list.pop()
gLock.release()
filename = img_url.split('?')[0].split('/')[-1]
r = requests.get(img_url)
print('正在下载:', filename)
path = './picture/' + filename
with open(path,'wb') as f:
f.write(r.content)
f.close()
if len(img_links_list) == 0:
end = time.time()
print("消耗的时间为:", (end - start))
exit()
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
最后的代码就是启动线程
for x in range(5):
Generant().start()
for x in range(5):
Consumer().start()