繁体   English   中英

如何使用python的httplib2保存文件?

[英]How do I save a file using python's httplib2?

我可以运行以下代码:

import httplib2

h = httplib2.Http('.cache')

response, content = h.request('http://2.bp.blogspot.com/-CXFfl9luHPM/TV-Os6opQfI/AAAAAAAAA2E/oCgrgvWqzrY/s1600/cow.jpg')

print(response.status)

with open('cow.jpg', 'wb') as f:
    f.write(content)

运行代码时,我下载了一个名为cow.jpg的文件,但我也得到了一个重复的图像,其名称不同:2.bp.blogspot.com,-CXFfl9luHPM,TV-Os6opQfI,AAAAAAAAA2E, oCgrgvWqzrY,S1600,cow.jpg,77ba31012a25509bfdc78bea4e1bfdd1。 这是带有逗号和其他垃圾内容的http地址。 关于如何使用httplib2仅创建一个映像的任何想法? 谢谢。

只需将内容写入文件:

with open('cow.jpg', 'wb') as f:
    f.write(content)

使用urllib和urlretrieve方法,第二个参数是文件位置。

对于python 2.x

import urllib
urllib.urlretrieve(URL, path_destination)

您也可以使用urllib2吗? 如果是,则可以使用以下功能:

def download_file(url):
    """Create an urllib2 request and return the request plus some useful info"""
    name = filename_from_url(url)
    r = urllib2.urlopen(urllib2.Request(url))
    info = r.info()
    if 'Content-Disposition' in info:
        # If the response has Content-Disposition, we take filename from it
        name = info['Content-Disposition'].split('filename=')[1]
        if name[0] == '"' or name[0] == "'":
            name = name[1:-1]
    elif r.geturl() != url:
        # if we were redirected, take the filename from the final url
        name = filename_from_url(r.geturl())
    content_type = None
    if 'Content-Type' in info:
        content_type = info['Content-Type'].split(';')[0]
    # Try to guess missing info
    if not name and not content_type:
        name = 'unknown'
    elif not name:
        name = 'unknown' + mimetypes.guess_extension(content_type) or ''
    elif not content_type:
        content_type = mimetypes.guess_type(name)[0]
    return r, name, content_type

用法:

fp, filename, content_type = download_file('http://url/to/some/file')
with open('somefile', 'w') as dst:
    shutil.copyfileobj(fp, dst)

该代码的优点是永远不会将整个文件读入内存-因此,它对于大型文件也能正常工作。 除此之外,它还为您提供了从服务器接收的文件名和内容类型(如果需要)。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM