python調用ie抓取網頁里的圖片并保存
這段代碼調用ie瀏覽器下載網頁上的所有圖片,并保存到本地
import win32com.client,time,win32inet,win32file,os class ImgDownloader: def init(self,url,dir): self.dir=dir self.ie=win32com.client.Dispatch('InternetExplorer.Application.1') self.ie.Navigate(url) self.ie.Visible = 1 self.wait()def __wait__(self): while self.__ie.Busy: time.sleep(0.5) def start(self): self.__wait__() imgs=self.__ie.Document.getElementsByTagName('img') for i in range(imgs.length): try: cachInfo=win32inet.GetUrlCacheEntryInfo(imgs[i].src) if cachInfo: path=cachInfo['LocalFileName'] pathinfo=path.split('\\') pathinfo.reverse() filename=('[%d]' % i) + pathinfo[0] win32file.CopyFile(path,os.path.join(self.__dir,filename),True) except: pass def close(self): self.__ie.Quit()
if name=='main': d=ImgDownloader('http://www.baiduhome.net','E:\\temp\\') d.start() d.close() </pre>
本文由用戶 mxw8 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!