python抓取页面图片

LFSYue 2012-08-10 04:12:10
在http://www.cnblogs.com/flysun/archive/2009/06/16/1504278.html找了个程序,代码如下:
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.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()
在IDLE中运行的时候:
Traceback (most recent call last):
File "E:\catch.py", line 33, in <module>
d=ImgDownloader('http://www.hao123.com/','E:\\temp\\')
File "E:\catch.py", line 5, in __init__
self.__ie=win32com.client.Dispatch('InternetExplorer.Application')
File "C:\Python32\lib\site-packages\win32com\client\__init__.py", line 95, in Dispatch
dispatch, userName = dynamic._GetGoodDispatchAndUserName(dispatch,userName,clsctx)
File "C:\Python32\lib\site-packages\win32com\client\dynamic.py", line 108, in _GetGoodDispatchAndUserName
return (_GetGoodDispatch(IDispatch, clsctx), userName)
File "C:\Python32\lib\site-packages\win32com\client\dynamic.py", line 85, in _GetGoodDispatch
IDispatch = pythoncom.CoCreateInstance(IDispatch, None, clsctx, pythoncom.IID_IDispatch)
pywintypes.com_error: (-2147024893, '系统找不到指定的路径。', None, None)
请问如何解决问题??
...全文
1067 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
LFSYue 2012-08-13
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 的回复:]

没啥问题了,关键运行代码时候,最好关闭浏览器,而且默认浏览器是IE
Python code
import win32com.client,time,win32inet,win32file,os
class ImgDownloader:
def __init__(self,url,dir):
self.__dir=dir
self.__ie=win32c……
[/Quote]

哦,原来是这样,我没装IE,也难怪,谢谢你了!!!
Gloveing 2012-08-13
  • 打赏
  • 举报
回复
没啥问题了,关键运行代码时候,最好关闭浏览器,而且默认浏览器是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.hao123.com/','E:\\temp\\')
d.start()
d.close()













LFSYue 2012-08-13
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 的回复:]

代码重新发一下吧,你上面的代码太乱了。。
[/Quote]

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']
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.hao123.com/','E:\\temp\\')
d.start()
d.close()
Gloveing 2012-08-12
  • 打赏
  • 举报
回复
代码重新发一下吧,你上面的代码太乱了。。
LFSYue 2012-08-11
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 的回复:]

引用 3 楼 的回复:

引用 1 楼 的回复:

self.__ie=win32com.client.Dispatch('InternetExplorer.Application.1')
而且最好你在运行这个程序的时候,要关闭所有浏览器

还是不行

先不要保存图片,你试试能否通过这个脚本打印出一个网页的源码?
[/Quote]

你能试试最好了,呵呵
Gloveing 2012-08-10
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 的回复:]

引用 1 楼 的回复:

self.__ie=win32com.client.Dispatch('InternetExplorer.Application.1')
而且最好你在运行这个程序的时候,要关闭所有浏览器

还是不行
[/Quote]
先不要保存图片,你试试能否通过这个脚本打印出一个网页的源码?
LFSYue 2012-08-10
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 的回复:]

self.__ie=win32com.client.Dispatch('InternetExplorer.Application.1')
而且最好你在运行这个程序的时候,要关闭所有浏览器
[/Quote]
还是不行
LFSYue 2012-08-10
  • 打赏
  • 举报
回复
ok,我试试
Gloveing 2012-08-10
  • 打赏
  • 举报
回复
self.__ie=win32com.client.Dispatch('InternetExplorer.Application.1')
而且最好你在运行这个程序的时候,要关闭所有浏览器

37,721

社区成员

发帖
与我相关
我的任务
社区描述
JavaScript,VBScript,AngleScript,ActionScript,Shell,Perl,Ruby,Lua,Tcl,Scala,MaxScript 等脚本语言交流。
社区管理员
  • 脚本语言(Perl/Python)社区
  • IT.BOB
加入社区
  • 近7日
  • 近30日
  • 至今

试试用AI创作助手写篇文章吧