python调用ie抓取网页里的图片并保存
2018-07-20 来源:open-open
这段代码调用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.open-open.com','E:\\temp\\')
d.start()
d.close()
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点!
本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。
最新资讯
热门推荐