0

我正在用这个从命令行执行python脚本

python myscript.py

这是我的脚本

if item['image_urls']:
            for image_url in item['image_urls']:
            subprocess.call(['wget','-nH', image_url, '-P  images/'])

现在,当我在屏幕上运行时,我看到这样的输出

HTTP request sent, awaiting response... 200 OK
Length: 4159 (4.1K) [image/png]

现在我想要的是终端上不应该有输出。

我想抓取输出并从那里找到图像扩展名,即从[image/png]抓取png文件并将文件重新命名为something.png

这可能吗

4

1 回答 1

3

如果您只想使用 下载东西wget,为什么不在标准 python 库中尝试urllib.urlretrieve呢?

import os
import urllib
image_url = "https://www.google.com/images/srpr/logo3w.png"
image_filename = os.path.basename(image_url)
urllib.urlretrieve(image_url, image_filename)

编辑:如果图像由脚本动态重定向,您可以尝试requests使用包来处理重定向。

import requests
r = requests.get(image_url)
# here r.url will return the redirected true image url
image_filename = os.path.basename(r.url)
f = open(image_filename, 'wb')
f.write(r.content)
f.close()

因为找不到合适的测试用例,所以我没有测试代码。一大优点requests是它还可以处理授权

EDIT2:如果图像由脚本动态提供,例如gravatar图像,您通常可以在响应标头的content-disposition字段中找到文件名。

import urllib2
url = "http://www.gravatar.com/avatar/92fb4563ddc5ceeaa8b19b60a7a172f4"
req = urllib2.Request(url)
r = urllib2.urlopen(req)
# you can check the returned header and find where the filename is loacated
print r.headers.dict
s = r.headers.getheader('content-disposition')
# just parse the filename
filename = s[s.index('"')+1:s.rindex('"')]
f = open(filename, 'wb')
f.write(r.read())
f.close()

EDIT3:正如@Alex 在评论中建议的那样,您可能需要清理filename返回的标头中的编码,我认为只需获取基本名称就可以了。

import os
# this will remove the dir path in the filename
# so that `../../../etc/passwd` will become `passwd`
filename = os.path.basename(filename)
于 2012-12-28T07:00:34.117 回答