gpt4 book ai didi

Python FTP 抓取和保存图像问题

转载 作者:太空宇宙 更新时间:2023-11-04 06:30:54 24 4
gpt4 key购买 nike

编辑:我让它正常工作,只是不会下载任何东西...

现在我的代码简化了:

notions_ftp = ftplib.FTP(ftp_host, ftp_user, ftp_passwd)
folder = "Leisure Arts - Images"
notions_ftp.cwd(folder)
image = open("015693PR-com.jpg","wb")
notions_ftp.retrlines("RETR 015693PR-com.jpg", image.write)
send_image = open("015693PR-com.jpg", 'r')

这是我的输出:

'250 "/Leisure Arts - Images": is current directory.'
'226 Transfer complete. 0 bytes in 0.00 sec. (0.000 Kb/s)'

原帖:好的,所以我整天都在搞乱这个。我是 Python FTP 的新手。所以我在这里搜索并找到了这个:

images = notions_ftp.nlst()
for image_name in image_names:
if found_url == False:
try:
for image in images:
ftp_image_name = "./%s" % image_name
if ftp_image_name == image:
found_url = True
image_name_we_want = image_name
except:
pass

# We failed to find an image for this product, it will have to be done manually
if found_url == False:
log.info("Image ain't there baby -- SKU: %s" % sku)
return False
# Hey we found something! Open the image....
notions_ftp.retrlines("RETR %s" % image_name_we_want, open(image_name_we_want, "rb"))
1/0

因此,我已将误差缩小到除以零之前的那一行。这是错误:

Traceback (most recent call last):
File "<console>", line 6, in <module>
File "<console>", line 39, in insert_image
IOError: [Errno 2] No such file or directory: '411483CC-IT,IM.jpg'

因此,如果您按照代码进行操作,您将看到图像在目录中,因为如果在我的代码第一行的目录列表中找到 image_name_we_want 则已设置。而且我知道它在那里,因为我自己正在查看 FTP 站点并且......它在那里很奇怪。因此,在所有这些过程中的某个时候,我将图像保存在本地,这是最需要的,但我早就忘记了我以前是怎么做到的。无论哪种方式,当它在列表中清楚地找到它时,为什么它认为图像不存在。

好的,所以我采纳了建议,现在我得到:

14:01:18,057 ERROR [pylons-admin] Image to big or corrupt! Skipping Image for product sku: 411483
14:01:18,057 ERROR [pylons-admin] Image to big or corrupt! Skipping Image for product sku: 411483
Traceback (most recent call last):
File "<console>", line 6, in <module>
File "<console>", line 40, in insert_image
File "/opt/local/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/ftplib.py", line 418, in retrlines
callback(line)
TypeError: 'file' object is not callable
>>> 14:01:24,694 INFO [pylons-admin] Script Done! Updated 0 Products w/ Images

所以它得到了第一个图像,我认为它确实已损坏,但是当它继续处理下一个要处理的图像时,它会在文件对象上抛出该错误。所以这里是我如何改变它的,其余的代码没有放在第一位:

# Hey we found something! Open the image....
f = open(image_name_we_want, "wb")
notions_ftp.retrlines("RETR %s" % image_name_we_want, f)
send_image = open(image_name_we_want, 'r')

# ...and send it for processing
try:
image_id = product_obj.set_image(send_image, 5, 1)
except IOError, error:
log.error("Image to big or corrupt! Skipping Image for product sku: %s" % sku)
image_id = False
else:
if image_id == False:
log.error("Could not Insert the image for product sku: %s" % sku)
f.close()
return False
else:
f.close()
os.remove(image_name_we_want)
return True

最佳答案

您正在读取文件而不是写入文件。

所以不要使用 open(image_name_we_want, "rb") 使用 open(image_name_we_want, "wb")

[编辑]如果你只是从 ftp 服务器获取数据,你也可以试试这个:

import urllib2
fh = urllib2.urlopen('ftp://server/path/file.png')
file('file.png', 'wb').write(fh.read())

此外,对于一个完整的工作示例,请阅读:http://docs.python.org/library/ftplib.html

您还缺少 open() 之后的 write

关于Python FTP 抓取和保存图像问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2915597/

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