python - 如何解析目录中的每个 html 文件中的图像?

标签 python image jpeg beautifulsoup

我有一个充满 html 文件的目录,每个文件中都有一张牛皮癣患者的临床图像。我想打开每个文件,找到图像,并将其保存在同一目录中。

import os, os.path
import Image
from BeautifulSoup import BeautifulSoup as bs

path = 'C:\Users\gokalraina\Desktop\derm images'

for root, dirs, files in path:
    for f in files:
        soup = bs(f)
        for image in soup.findAll("img"):
            print "Image: %(src)s" % image
            im = Image.open(image)
            im.save(path+image["src"], "JPEG")

我收到此错误:

 Traceback (most recent call last):
   File "C:\Users\gokalraina\Desktop\modfile.py", line 7, in <module>
     for root, dirs, files in path:
 ValueError: need more than 1 value to unpack

即使在谷歌上搜索错误后,我也不知道出了什么问题或者我是否正确地执行了此操作。请记住,我是 python 新手。

编辑:对程序进行建议的更改后,我仍然收到错误:

  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")

最佳答案

您需要更改此行:

for root, dirs, files in path:

for root, dirs, files in os.walk(path):

另请注意,文件是文件名称,而不是对象,因此这将是您的固定代码:

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")

关于python - 如何解析目录中的每个 html 文件中的图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9608683/

相关文章:

html - 图像 - 单选按钮

compression - 为什么 JPEG 压缩按 8x8 block 处理图像?

java - PHP - 如何将字节数组转换为图像文件?

algorithm - 如何缩小 JPEG 图像以使文本尽可能清晰?

python - 在 win7 上的 eclipse 中创建一个 django 项目?

python - Pandas 前一行中连续出现的次数

python - Tensorflow 属性错误 : 'numpy.float32' object has no attribute 'value'

python - 训练数据的拟合变换和测试数据的变换

html - 仍然渲染 png 图像背景黑色

javascript - 使用脚本更改元素的尺寸