自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

Python抓取網(wǎng)頁圖片相關代碼編寫方法

開發(fā) 后端
我們今天將會通過對一段代碼的解讀來為大家詳細介紹一下Python抓取網(wǎng)頁圖片的相關操作方法,希望大家可以從中掌握這一應用技術。

利用Python編程語言進行網(wǎng)頁內(nèi)容的抓取是一個比較常用的編程技術。那么,今天我們將會為大家詳細介紹一下有關Python抓取網(wǎng)頁圖片的操作方法,以方便大家在實際應用中獲得一些幫助。

Python抓取網(wǎng)頁圖片代碼示例:

  1. ImgDownloader  
  2. import win32com.client,time,win32inet,win32file,os  
  3. class ImgDownloader:  
  4. def __init__(self,url,dir):  
  5. self.__dir=dir  
  6. self.__ie=win32com.client.Dispatch('InternetExplorer.Application')  
  7. self.__ie.Navigate(url)  
  8. self.__wait__()  
  9. def __wait__(self):  
  10. while self.__ie.Busy:  
  11. time.sleep(0.1)  
  12. def start(self):  
  13. self.__wait__()  
  14. imgs=self.__ie.Document.getElementsByTagName('img')  
  15. for i in range(imgs.length):  
  16. try:  
  17. cachInfo=win32inet.GetUrlCacheEntryInfo(imgs[i].src)  
  18. if cachInfo:  
  19. path=cachInfo['LocalFileName']  
  20. pathpathinfo=path.split('\\')  
  21. pathinfo.reverse()  
  22. filename=('[%d]' % i) + pathinfo[0]  
  23. win32file.CopyFile(path,os.path.join(self.__dir,filename),True)  
  24. except:  
  25. pass  
  26. def close(self):  
  27. self.__ie.Quit()  
  28. if __name__=='__main__':  
  29. d=ImgDownloader('http://image.baidu.com/i?ct=201326592&cl=2&
    lm=-1&tn=baiduimage&pv=&word=boy&z=0','c:\\temp\\')  
  30. d.start()  
  31. d.close() 

原理:在Python使用com 接口運行IE瀏覽器,然后打開網(wǎng)頁,獲取網(wǎng)頁所有圖片的URL,最后利用win32api函數(shù)GetUrlCacheEntryInfo找出圖片相應的本地緩存文件,復制到指定目錄。

以上就是我們?yōu)榇蠹医榻B的Python抓取網(wǎng)頁圖片的應用方式。

【編輯推薦】

  1. Python AOP正確實現(xiàn)方法介紹
  2. Python解釋器正確調(diào)用方式簡介
  3. Python開發(fā)環(huán)境相關搭建方法介紹
  4. windows service運行Python相關操作技巧分享
  5. Python下劃線在實際應用中功能體現(xiàn)
責任編輯:曹凱 來源: 博客園
相關推薦

2010-03-03 15:39:50

Python抓取網(wǎng)頁內(nèi)

2010-03-12 11:07:49

Python retu

2019-07-24 16:00:37

Python代碼高清圖片

2010-01-18 11:03:17

VB.NET網(wǎng)頁計數(shù)器

2009-12-02 17:22:22

PHP抓取圖片

2012-05-17 15:11:23

linux

2009-12-30 14:44:04

Silverlight

2021-11-24 17:22:06

網(wǎng)絡抓取網(wǎng)絡爬蟲數(shù)據(jù)收集

2009-12-30 14:51:47

Silverlight

2023-03-09 15:55:17

JavaScriptURLCSS

2009-12-01 18:47:19

PHP代碼轉義

2009-07-31 10:34:41

ASP.NET抓取網(wǎng)頁

2009-12-02 15:50:41

PHP抓取網(wǎng)頁內(nèi)容

2025-04-03 02:35:00

GoogleGemini工具

2009-09-07 14:00:57

C#抓取網(wǎng)頁

2010-01-27 17:53:18

Android顯示網(wǎng)絡

2010-02-03 13:55:51

Python 代碼

2010-01-11 14:16:14

VB.NET生成驗證碼

2020-10-12 08:19:43

Python爬蟲網(wǎng)頁數(shù)據(jù)

2009-08-05 16:40:51

Gestalt
點贊
收藏

51CTO技術棧公眾號