python批量下载图⽚的三种⽅法有三种⽅法,⼀是⽤微软提供的扩展库win32com来操作IE,⼆是⽤selenium的webdriver,三是⽤python⾃带的HTMLParser 解析。
win32com可以获得类似js⾥⾯的document对象,但貌似是只读的(⽂档都没找到)。
selenium则提供了Chrome,IE,FireFox等的⽀持,每种浏览器都有execute_script和find_element_by_xx⽅法,可以⽅便的执⾏js脚本(包括修改元素)和读取html⾥⾯的元素。
不⾜是selenium只提供对python2.6和2.7的⽀持。
HTMLParser则是需要⾃⼰写个类继承基类,重写解析元素的⽅法。
个⼈感觉selenium⽤起来更⽅便,很容易操作html⾥的元素。
代码如下:win32com:复制代码代码如下:#将滚动条滑到底,最多滑动20000像素#模拟键盘右键,查看多张图⽚import sysimport win32com.client,win32apiimport urllib.requestimport timeimport osdef main():#获取参数url=sys.argv[1]#操作IEie=win32com.client.Dispatch("InternetExplorer.Application")ie.Navigate(url)ie.Visible=Truelast_url=''dir_name=''while last_url!=url:print('\nThe URL is:',url,'\n')while ie.ReadyState != 4:time.sleep(1)while ie.Document.readyState != "complete":time.sleep(1)#滑动滚动条win=ie.Document.parentWindowlastY=-1;for i in range(40):win.scrollTo(0,500*i)nowY=win.pageYOffsetif(nowY==lastY):breaklastY=nowYtime.sleep(0.4)print('Document load state:',ie.Document.readyState)doc=ie.Document#第⼀次需要创建⽬录if(dir_name==''):root_dir='E:\\img'dir_name=root_dir+'\\'+doc.titledir_name=dir_name.replace('|','-')if(os.path.exists(root_dir)!=True):os.mkdir(root_dir)if(os.path.exists(dir_name)!=True):os.mkdir(dir_name)all_image=doc.imagesprint('共有',all_image.length,'张图⽚')count=0;for img in all_image:if(img.id=='b_img'):count=count+1print(count,img.src)time.sleep(1)img_file=urllib.request.urlopen(img.src)byte=img_file.read()print(count,'donwload complete!','-'*10,'size:','{:.3}'.format(byte.__len__()/1024),'KB') if(byte.__len__()>7000):file_name=img.src.replace('/','_')file_name=file_name.replace(':','_')end=file_name.__len__()if(file_name.rfind('!')!=-1):end=file_name.rfind('!')if(file_name.rfind('?')!=-1):end=file_name.rfind('?')file_name=file_name[:end]write_file=open(dir_name+'\\'+file_name,'wb')write_file.write(byte)write_file.close()print(count,file_name,'complete!')#下⼀张last_url=urlwin32api.keybd_event(39,0)time.sleep(1)url=ie.Document.urlprint(last_url,url)#ie.Quit()if __name__ == '__main__':main()selenium:复制代码代码如下:# -*- coding: cp936 -*-import sysimport urllibimport timeimport osfrom selenium import webdriverdef main():#获取参数url=sys.argv[1]#操作IEdriver=webdriver.Chrome()driver.get(url)driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")#创建⽬录dir_name=driver.find_element_by_tag_name('title').textprint dir_nameroot_dir='E:\\img'dir_name=root_dir+'\\'+dir_namedir_name=dir_name.replace('|','-')if(os.path.exists(root_dir)!=True):os.mkdir(root_dir)if(os.path.exists(dir_name)!=True):os.mkdir(dir_name)images=driver.find_elements_by_tag_name('img')count=0for image in images:count=count+1image_url=str(image.get_attribute('src'))img_file=urllib.urlopen(image_url)byte=img_file.read()print count,'donwload complete!','-'*10,'size:',byte.__len__()/1024,'KB'if(byte.__len__()>7000):file_name=image_url.replace('/','_')file_name=file_name.replace(':','_')end=file_name.__len__()if(file_name.rfind('!')!=-1):end=file_name.rfind('!')if(file_name.rfind('?')!=-1):end=file_name.rfind('?')file_name=file_name[:end]write_file=open(dir_name+'\\'+file_name,'wb')write_file.write(byte)write_file.close()print count,file_name,'complete!'driver.quit()if __name__ == '__main__':main()HTMLParser:复制代码代码如下:# import modules used here -- sys is a very standard oneimport sysimport urllib.request# Gather our code in a main() functionfrom html.parser import HTMLParserclass MyHTMLParser(HTMLParser):def handle_starttag(self,tag,attrs):if(tag=='img'):for attr in attrs:if(attr[0]=='src'):img_file=urllib.request.urlopen(attr[1])byte=img_file.read()#⽂件⼤于1000b则⽣成⽂件,添加计数,下载多少图⽚,显⽰html代码 if(byte.__len__()>1000):file_name=attr[1].replace('/','_')file_name=file_name.replace(':','_')end=file_name.__len__()if(file_name.rfind('!')!=-1):end=file_name.rfind('!')if(file_name.rfind('?')!=-1):end=file_name.rfind('?')file_name=file_name[:end]## print(file_name)write_file=open('E:\\img\\'+file_name,'wb')write_file.write(byte)write_file.close()def main():#获取参数url=sys.argv[1]print('\nThe URL is:',url,'\n')#读取url所指向的资源html_file=urllib.request.urlopen(url)byte_content=html_file.read()#将html⽹页保存起来url_file=open('E:\\img\\html\\result.htm','wb')url_file.write(byte_content)url_file.close()#从字节转换为字符串s=str(byte_content, encoding = "utf-8")#print(s)#bytes.decode(html_file.read())parser=MyHTMLParser(strict=False)parser.feed(s)# Standard boilerplate to call the main() function to begin # the program.if __name__ == '__main__':main()。