Python 无法打开使用Beauty soup库下载的图像
我有一个脚本,可以使用BeautifulSoup库从网页下载图像。当我使用诸如之类的网站时,图像会正确下载到我桌面上的文件夹中,我可以打开并查看它。但是,当我使用一个站点时,例如,a图像显示下载到正确的文件夹桌面,但我收到一条错误消息,说“Paint无法读取此文件。这不是有效的位图文件,或者其格式当前不受支持。”我想这可能与google主页html文件中的文件路径是相对的这一事实有关,它是/images/srpr/logo4w.png,其中包含的图像路径不是相对的,而是/rsrc/1370373631437/one-1/Test.png">https://sites.google.com/site/imagesizetesting//rsrc/1370373631437/one-1/Test.png. 我不知道图像来源的不同是什么原因造成的,还是其他原因造成的。有什么想法吗?这是我解析和下载图像的代码Python 无法打开使用Beauty soup库下载的图像,python,beautifulsoup,Python,Beautifulsoup,我有一个脚本,可以使用BeautifulSoup库从网页下载图像。当我使用诸如之类的网站时,图像会正确下载到我桌面上的文件夹中,我可以打开并查看它。但是,当我使用一个站点时,例如,a图像显示下载到正确的文件夹桌面,但我收到一条错误消息,说“Paint无法读取此文件。这不是有效的位图文件,或者其格式当前不受支持。”我想这可能与google主页html文件中的文件路径是相对的这一事实有关,它是/images/srpr/logo4w.png,其中包含的图像路径不是相对的,而是/rsrc/1370373
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()
我知道了!这是我的更新代码,以防其他人有类似的问题
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)
#Old File path (local to computer)
#image["src"] = '%s\%s' % (out,filename)
print 'New Path: %s' % image["src"]
# print image
outpath = os.path.join(out, filename)
#retrieve images
if parsedURL[2].lower().startswith("http"):
#urlretrieve(image["src"], outpath)
urlretrieve(parsedURL[2], outpath)
print image["src"].lower()
else:
print "HTTP INFO " + urlparse.urlunparse(parsedURL)
print "HTTP INFO " + image["src"].lower()
urlretrieve(urlparse.urlunparse(parsedURL), outpath) #Constructs URL from tuple (parsedURL)
#print image["src"].lower()
如果你将图片另存为浏览器,并将其存储在HD中,Paint是否能够打开它?我已经找到了答案。保罗,谢谢你的帮助。