简体   繁体   English

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

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

I can run the following code: 我可以运行以下代码:

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)

When I run the code, I download a file called cow.jpg which is what I want, but I also get a duplicate image with a different name called: 2.bp.blogspot.com,-CXFfl9luHPM,TV-Os6opQfI,AAAAAAAAA2E,oCgrgvWqzrY,s1600,cow.jpg,77ba31012a25509bfdc78bea4e1bfdd1. 运行代码时,我下载了一个名为cow.jpg的文件,但我也得到了一个重复的图像,其名称不同:2.bp.blogspot.com,-CXFfl9luHPM,TV-Os6opQfI,AAAAAAAAA2E, oCgrgvWqzrY,S1600,cow.jpg,77ba31012a25509bfdc78bea4e1bfdd1。 It's the http address with commas plus other junk. 这是带有逗号和其他垃圾内容的http地址。 Any ideas on how I can create only one image using httplib2? 关于如何使用httplib2仅创建一个映像的任何想法? Thanks. 谢谢。

Just write the content to a file: 只需将内容写入文件:

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

Use urllib and method urlretrieve, the second argument is the file location. 使用urllib和urlretrieve方法,第二个参数是文件位置。

for python 2.x 对于python 2.x

import urllib
urllib.urlretrieve(URL, path_destination)

Is using urllib2 ok for you, too? 您也可以使用urllib2吗? If yes, you can use this function: 如果是,则可以使用以下功能:

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

Usage: 用法:

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

This code has the advantage that is never reads the whole file into memory - so it works fine for huge files, too. 该代码的优点是永远不会将整个文件读入内存-因此,它对于大型文件也能正常工作。 Besides that, it also gives you the filename received from the server and the content-type in case you want/need it. 除此之外,它还为您提供了从服务器接收的文件名和内容类型(如果需要)。

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

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