這是這個問題的後續:How do I parse every html file in a directory for images? 本質上,我有一個html文件的目錄,其中每個包含圖像,我想單獨保存在同一個目錄中。如何從目錄中的html文件中提取圖像?
使該程序所建議的修改之後,我仍然得到一個錯誤:
Image: theme/pfeil_grau.gif
Traceback (most recent call last):
File "C:\Users\gokalraina\Desktop\modfile.py", line 25, in <module>
im = Image.open(image)
File "C:\Python27\lib\site-packages\PIL\Image.py", line 1956, in open
prefix = fp.read(16)
TypeError: 'NoneType' object is not callable
這是我使用修改後的代碼(感謝nightcracker)。
import os, os.path
import Image
from BeautifulSoup import BeautifulSoup as bs
path = 'C:\Users\gokalraina\Desktop\derm images'
for root, dirs, files in os.walk(path):
for f in files:
soup = bs(open(os.path.join(root, f)).read())
for image in soup.findAll("img"):
print "Image: %(src)s" % image
im = Image.open(image)
im.save(path+image["src"], "JPEG")
請包括整個輸出到這一點,其中包括一個用於'打印「圖片:%s的」%image'位。 – 2012-03-08 03:16:36
我已添加它;在打破之前只有一張照片。 – 2012-03-08 04:15:33