zoukankan      html  css  js  c++  java
  • 使用Python抓取网页图片

          今天写了一个Python小程序,用来抓取网页图片.

         


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

    原理:在Python使用com 接口运行IE浏览器,然后打开网页,获取网页所有图片的URL,最后利用win32api函数GetUrlCacheEntryInfo找出图片相应的本地缓存文件,复制到指定目录。

  • 相关阅读:
    Starting a Build
    getting the xml for running tfsreg.exe
    Monitoring and Analyzing Builds
    Getting Started with Silverlight
    Creating a New Build
    culture definition
    从数学到密码学(一)
    git如何建立分支实现新功能合并新功能到主分支
    git的patch解决二进制文件冲突生成某个文件的patch合并一个patch查看某个文件的历史比较两个文件
    blender 用户界面基本构成
  • 原文地址:https://www.cnblogs.com/flysun/p/1504278.html
Copyright © 2011-2022 走看看