gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 23:45:19 25 4
gpt4 key购买 nike

我有一个充满 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com