我有一个脚本,使用BeautifulSoup库从网页下载图像。当我使用诸如http://www.google.com之类的网站时,图像会正确下载到桌面上的文件夹中,然后我可以打开并查看它。但是,当我使用诸如https://sites.google.com/site/imagesizetesting/one-1这样的站点时,图像显示为下载到正确的文件夹桌面,但我收到一条错误消息,指出“Paint无法读取此文件。这不是有效的位图文件,或者其格式不是当前的支持的。”我认为这可能与谷歌主页的html文件中的文件路径是相对的,它是/images/srpr/logo4w.png,而包含在https://sites.google.com/site/imagesizetesting/one-1中的图像的路径不是相对的,它是/ rsrc /1370373631437/one-1/Test.png"> https://sites.google.com/site/imagesizetesting//rsrc/1370373631437/one-1/Test.png。我不知道该如何区分对于图像源是什么导致它,或者它是别的东西。任何想法?这里是我的解析代码,并下载图像。无法打开使用美丽的汤库下载的图像
for image in soup.findAll("img"):
print "Old Image Path: %(src)s" % image
#Get file name
filename = image["src"].split("/")[-1]
#Get full path name if url has to be parsed
parsedURL[2] = image["src"]
image["src"] = '%s\%s' % (phonepath,filename)
print 'New Path: %s' % image["src"]
outpath = os.path.join(out, filename)
#retrieve images
if image["src"].lower().startswith("http"):
urlretrieve(image["src"], outpath)
print image["src"].lower()
else:
urlretrieve(urlparse.urlunparse(parsedURL), outpath) #Constructs URL from tuple (parsedURL)
print image["src"].lower()
如果您在浏览器中保存图片并将其存储在HD中,Paint是否可以打开它? –
是的,我能够。 – johns4ta
我想通了。感谢Paulo的帮助。 – johns4ta