1 #将滚动条滑到底,最多滑动20000像素
2 #模拟键盘右键,查看多张图片
3 import sys
4 import win32com.client,win32api
5 import urllib.request
6 import time
7 import os
8 def main():
9 #获取参数
10 url=sys.argv[1]
11 #操作IE
12 ie=win32com.client.Dispatch("InternetExplorer.Application")
13 ie.Navigate(url)
14 ie.Visible=True
15 last_url=''
16 dir_name=''
17 while last_url!=url:
18 print('\nThe URL is:',url,'\n')
19 while ie.ReadyState != 4:
20 time.sleep(1)
21 while ie.Document.readyState != "complete":
22 time.sleep(1)
23 #滑动滚动条
24 win=ie.Document.parentWindow
25 lastY=-1;
26 for i in range(40):
27 win.scrollTo(0,500*i)
28 nowY=win.pageYOffset
29 if(nowY==lastY):
30 break
31 lastY=nowY
32 time.sleep(0.4)
33 print('Document load state:',ie.Document.readyState)
34 doc=ie.Document
35 #第一次需要创建目录
36 if(dir_name==''):
37 root_dir='E:\\img'
38 dir_name=root_dir+'\\'+doc.title
39 dir_name=dir_name.replace('|','-')
40 if(os.path.exists(root_dir)!=True):
41 os.mkdir(root_dir)
42 if(os.path.exists(dir_name)!=True):
43 os.mkdir(dir_name)
44 all_image=doc.images
45 print('共有',all_image.length,'张图片')
46 count=0;
47 for img in all_image:
48 if(img.id=='b_img'):
49 count=count+1
50 print(count,img.src)
51 time.sleep(1)
52 img_file=urllib.request.urlopen(img.src)
53 byte=img_file.read()
54 print(count,'donwload complete!','-'*10,'size:','{:.3}'.format(byte.__len__()/1024),'KB')
55 if(byte.__len__()>7000):
56 file_name=img.src.replace('/','_')
57 file_name=file_name.replace(':','_')
58 end=file_name.__len__()
59 if(file_name.rfind('!')!=-1):
60 end=file_name.rfind('!')
61 if(file_name.rfind('?')!=-1):
62 end=file_name.rfind('?')
63 file_name=file_name[:end]
64 write_file=open(dir_name+'\\'+file_name,'wb')
65 write_file.write(byte)
66 write_file.close()
67 print(count,file_name,'complete!')
68 #下一张
69 last_url=url
70 win32api.keybd_event(39,0)
71 time.sleep(1)
72 url=ie.Document.url
73 print(last_url,url)
74 #ie.Quit()
75 if __name__ == '__main__':
76 main()