python制作微博圖片爬取工具
有小半個(gè)月沒(méi)有發(fā)博客了,因?yàn)橐恢痹谘芯縫ython的GUI,買(mǎi)了一本書(shū)學(xué)習(xí)了一些基礎(chǔ),用我所學(xué)做了我的第一款GUI——微博圖片爬取工具。本軟件源代碼已經(jīng)放在了博客中,另外軟件已經(jīng)打包好上傳到網(wǎng)盤(pán)中以供下載學(xué)習(xí)。
一.準(zhǔn)備工作本次要用到以下依賴庫(kù):re json os random tkinter threading requests PIL 其中后兩個(gè)需要安裝后使用
二.預(yù)覽1.啟動(dòng)這里只將拿一張圖片作為展示。
設(shè)計(jì)流程分為總體設(shè)計(jì)和詳細(xì)設(shè)計(jì),這里我會(huì)使用viso畫(huà)出幾個(gè)流程圖,用以展示我的思路,其中詳細(xì)設(shè)計(jì)部分,我列舉了兩個(gè)函數(shù)實(shí)現(xiàn)的具體流程。
1.總體設(shè)計(jì)此圖為整個(gè)系統(tǒng)的整體流程也是本GUI軟件的使用過(guò)程。
2.詳細(xì)設(shè)計(jì)在此列舉兩個(gè)函數(shù)一個(gè)是搜索按鈕觸發(fā)的wb_search函數(shù),一個(gè)是開(kāi)始爬取按鈕觸發(fā)的wb_pics_parse函數(shù)。
2.1wb_search函數(shù)
2.2wb_pics_parse函數(shù)
import jsonimport randomimport reimport osfrom tkinter import *from tkinter import messageboxfrom tkinter import ttkimport requestsimport threadingfrom PIL import Image,ImageTk'''1.07使用check button 實(shí)現(xiàn)下載完打開(kāi)文件夾操作,注冊(cè)了enter、esc熱鍵,優(yōu)化了一些體驗(yàn)1.08 1.更新了關(guān)鍵字、磁盤(pán)、用戶判斷邏輯 2.將之前的線程池改為多線程來(lái)執(zhí)行下載操作1.13說(shuō)明:如果在下載過(guò)程變慢,可能是軟件正在解析圖片地址或者就是您的網(wǎng)絡(luò)不行'''class WeiBo_pics_Spider(object): def __init__(self,start_url): self.start_url=start_url #解析出圖片地址 def get_pics_url(self): i = 1 global a_flag a_flag = True while True: url = self.start_url + ’&page={}’.format(i) headers = {’User-Agent’: get_ua()} r = requests.get(url, headers=headers) _json = json.loads(r.text) items = _json['data']['cards'] flag = _json[’ok’] if flag == 1 and a_flag: # 爬取數(shù)據(jù)標(biāo)志+一個(gè)手動(dòng)控制標(biāo)志for v in items: picslist = v.get(’mblog’) if picslist is not None: img_urls = picslist.get(’pics’) if img_urls != None: for img_url_ in img_urls:img_url = img_url_[’large’][’url’]yield img_url else:#1.06頁(yè)數(shù)顯示出現(xiàn)問(wèn)題t1.insert(END, f’***在第{i}頁(yè)終止***n’)t1.see(END)t1.update()if r1_var.get() == 1: big_dir=disk+’:/WeiBo_Pics’ os.startfile(big_dir)break i += 1 #下載圖片 def download_pics(self,url,filename): headers={’User-Agent’: get_ua()} r = requests.get(url, headers=headers) big_dir=disk+’:/WeiBo_Pics’ aim_path=big_dir+’/’+user_name_selected try: os.makedirs(aim_path) except: pass with open(aim_path + ’’ + filename, ’wb’)as f: f.write(r.content) # 保證焦點(diǎn)始終在最下 t1.see(END) # 下載完一張刷新一次 防止界面卡死崩潰 t1.insert(END, f’{filename}n’) window.update()def get_ua(): first_num = random.randint(55, 62) third_num = random.randint(0, 3200) fourth_num = random.randint(0, 140) os_type = [ ’(Windows NT 6.1; WOW64)’, ’(Windows NT 10.0; WOW64)’, ’(X11; Linux x86_64)’, ’(Macintosh; Intel Mac OS X 10_12_6)’ ] chrome_version = ’Chrome/{}.0.{}.{}’.format(first_num, third_num, fourth_num) ua = ’ ’.join([’Mozilla/5.0’, random.choice(os_type), ’AppleWebKit/537.36’, ’(KHTML, like Gecko)’, chrome_version, ’Safari/537.36’] ) return uadef wb_search(): #先清空l(shuí)sibox1內(nèi)容,便于新內(nèi)容顯示 listb1.delete(0,END) url1=’https://m.weibo.cn/api/container/getIndex?containerid=100103type%3D3%26q%3D{}%26t%3D0’ headers={’User-Agent’:get_ua()} key_word = e1.get() global user_id_list user_id_list=list() if len(key_word)!=0: #若用戶輸入了user_id,則去獲取screen_name if re.match(’d{10}’,key_word): user_id_list.append(key_word) url2 = f’https://m.weibo.cn/api/container/getIndex?uid={key_word}&containerid=100505{key_word}’ r1 = requests.get(url2, headers=headers) _data = json.loads(r1.text) screen_name = _data[’data’][’userInfo’].get(’screen_name’) l3.place(x=120, y=42) l3_var.set(f’搜索成功’) l3[’background’] = ’green’ listb1.insert(END, screen_name) #否則根據(jù)關(guān)鍵字去搜索用戶信息,顯示在listbox中 else: aim_url=url1.format(key_word) r=requests.get(aim_url,headers=headers) _json=json.loads(r.text) try:#若出現(xiàn)了IndexError則表明沒(méi)有檢索到用戶信息users=_json[’data’][’cards’][1].get(’card_group’)relevant_num=len(users)l3.place(x=105, y=42)l3_var.set(f’搜索到了 {relevant_num} 個(gè)用戶’)l3[’background’]=’green’for user_ in users: user_info=user_.get(’user’) user_name=user_info.get(’screen_name’) id = user_info.get(’id’) ''' 1.02的一種思路,使用一個(gè)列表存儲(chǔ)screen_name和uid,兩者用;(自定義字符,但應(yīng)避免較少?zèng)_突) 當(dāng)獲取Uid時(shí),直接切割字符串,取Listbox所選項(xiàng)索引,按索引在列表表值(uid) #使用字符串拼接 格式:screen_name+’;’+str(id) # user_data = user_name + ’;’ + str(id) ''' user_id_list.append(id) listb1.insert(END,user_name) except IndexError:#如果沒(méi)有檢索到用戶,就會(huì)報(bào)列表索引錯(cuò)誤messagebox.showinfo(title=’提示’, message=’沒(méi)有檢索到相關(guān)用戶,請(qǐng)更換關(guān)鍵字或使用用戶id搜索!’)l3.place(x=85, y=42)l3_var.set(f’請(qǐng)更換關(guān)鍵字或用戶id搜索!’)l3[’background’]=’yellow’#沒(méi)有檢索到用戶的話,提示之后,e1獲得焦點(diǎn)之后,清除用戶之前輸入e1.bind(’WM_TAKE_FOCUS’, e1_clear()) else:#處理沒(méi)有輸入關(guān)鍵字 messagebox.showinfo(title=’info’,message=’請(qǐng)輸入關(guān)鍵字!’) l3.place(x=110, y=42) l3_var.set(f’請(qǐng)輸入關(guān)鍵字!’) l3[’background’] = ’red’def wb_pics_parse(): key_word=e1.get() select_path=c1.get() #1.先判斷關(guān)鍵字是否輸入 if len(key_word)!=0: #2.再判斷是否選擇了磁盤(pán) if len(select_path)==1: #3.判斷所選路徑是否存在 if not os.path.exists(select_path):#4.判斷是否在列表框選擇了用戶名try: # 直接獲取選中項(xiàng)目 '''1.05獲取Listbox user_name_selected真費(fèi)勁''' global user_name_selected user_name_selected=listb1.get(listb1.curselection()) user_name_index = listb1.curselection()[0] user_id = user_id_list[user_name_index] container_id = ’107603’ + str(user_id) start_url = f’https://m.weibo.cn/api/container/getIndex?containerid={container_id}’ spider = WeiBo_pics_Spider(start_url) t1.config(state=’normal’) # 將Text開(kāi)啟,置為可讀可寫(xiě)狀態(tài) l3.place(x=120, y=42) l3_var.set(f’正在運(yùn)行......’) l3[’background’] = ’green’ for pic_url in spider.get_pics_url(): filename = pic_url.split(’/’)[-1] # 字符串切割,切割出前10個(gè)字符串 filename = filename[10:] thread_it(spider.download_pics,pic_url,filename)#搜索后,但是沒(méi)選擇用戶,會(huì)報(bào)TclError錯(cuò)誤,此except就用來(lái)捕獲這個(gè)異常except TclError: messagebox.showwarning(title=’警告’, message=’請(qǐng)選擇一個(gè)用戶!’) l3.place(x=105, y=42) l3_var.set(f’請(qǐng)選擇一個(gè)用戶!’) l3[’background’] = ’red’#獲取當(dāng)前選中項(xiàng)目(使用索引) else:messagebox.showwarning(title=’警告’,message=’請(qǐng)檢查路徑!’)l3.place(x=80, y=42)l3_var.set(f’請(qǐng)檢查路徑!’)l3[’background’] = ’red’ else: messagebox.showwarning(title=’警告’, message=’您未選擇磁盤(pán)!’) l3.place(x=85, y=42) l3_var.set(f’請(qǐng)檢查是否選擇了磁盤(pán)!’) l3[’background’] = ’red’ else: messagebox.showwarning(title=’警告’, message=’請(qǐng)輸入關(guān)鍵字!’) l3.place(x=110, y=42) l3_var.set(f’請(qǐng)輸入關(guān)鍵字!’) l3[’background’] = ’red’def open_disk(): disk=c1.get() big_dir=disk+’:/WeiBo_Pics’ if len(disk)==1: try: if not os.path.exists(big_dir):os.mkdir(big_dir) os.startfile(big_dir) except: messagebox.showwarning(title=’警告’,message=’選中的磁盤(pán)不存在!’) l3.place(x=110, y=42) l3_var.set(f’選中的磁盤(pán)不存在!’) l3[’background’] = ’red’ else: messagebox.showwarning(title=’警告’, message=’您未選中磁盤(pán)!’) l3.place(x=115, y=42) l3_var.set(f’您未選中磁盤(pán)!’) l3[’background’] = ’red’def window_quit(): ret=messagebox.askyesno(title=’提示’,message=’是否要退出?’) if ret==True: window.destroy() window.quit()def e1_clear(): e1.delete(0,END)def print_path(event): #要使用完整的路徑 global disk disk = c1.get() disk_path=c1.get()+’:/’ if len(disk)==1: if os.path.exists(disk_path): messagebox.showinfo(title=’提示’,message=f’文件將存儲(chǔ)到:{disk}:/WeiBo_Pics目錄下’) else: messagebox.showerror(title=’錯(cuò)誤’,message=’選定磁盤(pán)不存在!’) l3.place(x=100, y=42) l3_var.set(f’選中的磁盤(pán)不存在!’) l3[’background’] = ’red’ else: messagebox.showwarning(title=’警告’, message=’請(qǐng)先選定磁盤(pán)!’) l3.place(x=120, y=42) l3_var.set(f’請(qǐng)先選定磁盤(pán)!’) l3[’background’] = ’red’def switch(): if r1_var.get()==0: r1_var.set(1) else: r1_var.set(0)def escape(event): window_quit()def enter(event): wb_search()’’’解決程序卡死的重要方法,避免子線程和Ui線程在同一個(gè)線程’’’def thread_it(func, *args): ’’’將函數(shù)打包進(jìn)線程’’’ # 創(chuàng)建 t = threading.Thread(target=func, args=args) # 守護(hù) !!! t.setDaemon(True) # 啟動(dòng) t.start() # 阻塞--卡死界面! # t.join()window=Tk()width=310height=395screenWidth = window.winfo_screenwidth() # 獲取顯示區(qū)域的寬度screenHeight = window.winfo_screenheight() # 獲取顯示區(qū)域的高度left = (screenWidth - width) / 2top = (screenHeight - height) / 2window.geometry('%dx%d+%d+%d' % (width, height, left, top))window.resizable(0,0)window.title(’微博圖片采集工具-v1.08’)#設(shè)置圖標(biāo)ico_path=r’./rely/icon.ico’window.iconbitmap(ico_path)#插入圖片到Label中photo = Image.open('./rely/w_b.png') # 括號(hào)里為需要顯示在圖形化界面里的圖片photo = photo.resize((150, 40)) # 規(guī)定圖片大小img0 = ImageTk.PhotoImage(photo)l1=ttk.Label(window,imag=img0,justify=’center’)l1.pack()l3_var=StringVar()l3=ttk.Label(window,background=’yellow’,textvar=l3_var)l3.place(x=120,y=42)l3_var.set(’還沒(méi)搜索’)l1=ttk.Label(window,text=’關(guān)鍵字或n用戶id:’)l1.place(x=13,y=60)e1=ttk.Entry(window,justify=’center’)e1.place(x=80,y=65)l4=ttk.Label(window,text=’磁盤(pán):’)l4.place(x=13,y=100)disk_list=[’C’,’D’,’E’,’F’,’G’,’H’,’I’]c1=ttk.Combobox(window,justify=’center’,state=’readonly’,width=17,value=disk_list)#Combobox默認(rèn)選中索引為0的項(xiàng)目 即 C盤(pán)c1.bind(’<<ComboboxSelected>>’, print_path)c1.place(x=80,y=100)r1_var=IntVar()r1_var.set(1)#默認(rèn)選中為1check1=Checkbutton(window,text=’下載完n打開(kāi)文件夾’,command=switch)check1.place(x=223,y=90)b1=ttk.Button(window,text=’搜索’,command=lambda:thread_it(wb_search),width=7)b1.place(x=230,y=63)l5=ttk.Label(window,text=’用戶列表:’)l5.place(x=13,y=150)lb1_var=StringVar()listb1=Listbox(window,justify=’center’,listvariable=lb1_var,width=20,height=4)listb1.place(x=80,y=135)b2=ttk.Button(window,text=’開(kāi)始爬取’,command=lambda :thread_it(wb_pics_parse,),width=7)b2.place(x=230,y=160)l6=ttk.Label(window,text=’狀態(tài):’)l6.place(x=13,y=280)t1=Text(window,width=23,font=(’times new roman’,10),state=’disable’)t1.place(x=80,y=230,height=140)b3=ttk.Button(window,text=’ 打開(kāi)n文件夾’,width=7,command=open_disk)b3.place(x=230,y=230)b3=ttk.Button(window,text=’退出’,width=7,command=window_quit)b3.place(x=230,y=315)f1 = ttk.LabelFrame(window)f1.place(x=65,y=350)l6=ttk.Label(f1,text=’敬告:本軟件僅供學(xué)習(xí)交流使用!’,foreground=’red’)l6.pack(anchor='w',fill=X)#綁定esc鍵---退出window.bind(’<Escape>’,escape)#使用return鍵給輸入框Entry綁定enter事件---search搜索e1.bind(’<Return>’,enter)#加入主窗口銷(xiāo)毀事件window.protocol(’WM_DELETE_WINDOW’,window_quit)window.mainloop()五.總結(jié)說(shuō)明
本軟件僅供學(xué)習(xí)交流使用!圖源水印,在此僅作舉例!由于這是第一次做GUI,因此遇到了一些問(wèn)題,在此列舉一下:1.窗口布局問(wèn)題(GUI基礎(chǔ))2.主窗口執(zhí)行一個(gè)比較耗時(shí)操作導(dǎo)致卡死、崩潰(線程問(wèn)題)。3.主窗口關(guān)閉后,后臺(tái)線程還在運(yùn)行(線程問(wèn)題)。
以上問(wèn)題已經(jīng)全部解決,軟件切實(shí)可用。
另外,本軟件有四大亮點(diǎn):1.使用線程下載圖片2.智能標(biāo)簽提醒3.輸入關(guān)鍵字直接敲回車(chē)能夠完成搜索4.Esc快速退出軟件軟件打包好了放在了藍(lán)奏云https://wws.lanzous.com/iPSpzkchj5i
以上就是python制作微博圖片爬取工具的詳細(xì)內(nèi)容,更多關(guān)于python 微博圖片爬取的資料請(qǐng)關(guān)注好吧啦網(wǎng)其它相關(guān)文章!
相關(guān)文章:
1. idea修改背景顏色樣式的方法2. PHP腳本的10個(gè)技巧(8)3. IntelliJ IDEA配置Tomcat服務(wù)器的方法4. IntelliJ IDEA調(diào)整字體大小的方法5. IntelliJ IDEA設(shè)置默認(rèn)瀏覽器的方法6. idea設(shè)置自動(dòng)導(dǎo)入依賴的方法步驟7. jsp網(wǎng)頁(yè)實(shí)現(xiàn)貪吃蛇小游戲8. IntelliJ IDEA設(shè)置背景圖片的方法步驟9. python中復(fù)數(shù)的共軛復(fù)數(shù)知識(shí)點(diǎn)總結(jié)10. idea自定義快捷鍵的方法步驟
