Using the Python programming language to capture web content is a common programming technology. Today, we will introduce you in detail how to capture Web Images Using Python, so that you can get some help in practical applications.
Python web image capture code example:
- ImgDownloader
- import win32com.client,time,win32inet,win32file,os
- class ImgDownloader:
- def __init__(self,url,dir):
- self.__dir=dir
- self.__ie=win32com.client.Dispatch('InternetExplorer.Application')
- self.__ie.Navigate(url)
- self.__wait__()
- def __wait__(self):
- while self.__ie.Busy:
- time.sleep(0.1)
- 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']
- pathpathinfo=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://image.baidu.com/i?ct=201326592&cl=2&
lm=-1&tn=baiduimage&pv=&word=boy&z=0','c:\\temp\\')
- d.start()
- d.close()
Principle: Use the com interface in Python to run IE browser, open the webpage, obtain the URLs of all images on the webpage, and use the win32api function GetUrlCacheEntryInfo to find the corresponding local cache file of the image and copy it to the specified directory.
The above is how we use Python to capture web images.