File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ #-*- coding:UTF-8 -*-
2+ import os
3+ from time import time
4+
5+ import requests
6+ from bs4 import BeautifulSoup
7+ from queue import Queue
8+ from threading import Thread
9+
10+
11+ class DownloadBiaoqingbao (Thread ):
12+
13+ def __init__ (self , queue , path ):
14+ Thread .__init__ (self )
15+ self .queue = queue
16+ self .path = '/home/wistbean/biaoqingbao/'
17+ if not os .path .exists (path ):
18+ os .makedirs (path )
19+
20+ def run (self ):
21+ while True :
22+ url = self .queue .get ()
23+ try :
24+ # print(url)
25+ download_biaoqingbaos (url , self .path )
26+ finally :
27+ self .queue .task_done ()
28+
29+
30+ def download_biaoqingbaos (url , path ):
31+
32+ response = requests .get (url )
33+ soup = BeautifulSoup (response .content , 'lxml' )
34+ img_list = soup .find_all ('img' , class_ = 'ui image lazy' )
35+
36+ for img in img_list :
37+ image = img .get ('data-original' )
38+ title = img .get ('title' )
39+ print ('下载图片: ' , title )
40+
41+ try :
42+ with open (path + title + os .path .splitext (image )[- 1 ], 'wb' ) as f :
43+ img = requests .get (image ).content
44+ f .write (img )
45+ except OSError :
46+ print ('length failed' )
47+ break
48+
49+
50+ if __name__ == '__main__' :
51+
52+ start = time ()
53+
54+ # 构建所有的链接
55+ _url = 'https://fabiaoqing.com/biaoqing/lists/page/{page}.html'
56+ urls = [_url .format (page = page ) for page in range (1 , 4328 + 1 )]
57+
58+ queue = Queue ()
59+ path = '/home/wistbean/biaoqingbao/'
60+
61+ # 创建线程
62+ for x in range (10 ):
63+ worker = DownloadBiaoqingbao (queue , path )
64+ worker .daemon = True
65+ worker .start ()
66+
67+ # 加入队列
68+ for url in urls :
69+ queue .put (url )
70+
71+ queue .join ()
72+
73+ print ('下载完毕耗时: ' , time ()- start )
74+
75+
76+
Original file line number Diff line number Diff line change 1+ #-*- coding:UTF-8 -*-
2+ import glob
3+ import time
4+
5+ import itchat
6+ from itchat .content import TEXT , PICTURE
7+
8+
9+ imgs = []
10+
11+ def searchImage (text ):
12+ print ('收到关键词: ' , text )
13+ for name in glob .glob ('/home/wistbean/biaoqingbao/*' + text + '*.jpg' ):
14+ imgs .append (name )
15+
16+
17+ @itchat .msg_register ([PICTURE , TEXT ])
18+ def text_reply (msg ):
19+ searchImage (msg .text )
20+ for img in imgs [:6 ]:
21+ msg .user .send_image (img )
22+ time .sleep (0.3 )
23+ print ('开始发送表情: ' , img )
24+ imgs .clear ()
25+
26+
27+ itchat .auto_login (hotReload = True )
28+ itchat .run ()
You can’t perform that action at this time.
0 commit comments