如何使用 urllib2 在 python 中下载 zip 文件? [英] How do I download a zip file in python using urllib2?

查看:38
本文介绍了如何使用 urllib2 在 python 中下载 zip 文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

两部分问题.我正在尝试从互联网存档下载多个存档的 Cory Doctorow 播客.旧的没有进入我的 iTunes 提要.我已经编写了脚本,但下载的文件格式不正确.

Two part question. I am trying to download multiple archived Cory Doctorow podcasts from the internet archive. The old one's that do not come into my iTunes feed. I have written the script but the downloaded files are not properly formatted.

问题 1 - 下载 zip mp3 文件时需要更改什么?Q2 - 将变量传递到 URL 的更好方法是什么?

Q1 - What do I change to download the zip mp3 files? Q2 - What is a better way to pass the variables into URL?

 # and the base url.

def dlfile(file_name,file_mode,base_url):
    from urllib2 import Request, urlopen, URLError, HTTPError

    #create the url and the request
    url = base_url + file_name + mid_url + file_name + end_url 
    req = Request(url)

    # Open the url
    try:
        f = urlopen(req)
        print "downloading " + url

        # Open our local file for writing
        local_file = open(file_name, "wb" + file_mode)
        #Write to our local file
        local_file.write(f.read())
        local_file.close()

    #handle errors
    except HTTPError, e:
        print "HTTP Error:",e.code , url
    except URLError, e:
        print "URL Error:",e.reason , url

# Set the range 
var_range = range(150,153)

# Iterate over image ranges
for index in var_range:

    base_url = 'http://www.archive.org/download/Cory_Doctorow_Podcast_'
    mid_url = '/Cory_Doctorow_Podcast_'
    end_url = '_64kb_mp3.zip'
    #create file name based on known pattern
    file_name =  str(index) 
    dlfile(file_name,"wb",base_url

此脚本改编自此处

推荐答案

以下是我处理 url 构建和下载的方法.我确保将文件命名为 url 的基本名称(尾部斜杠后的最后一位),并且我还使用 with 子句打开要写入的文件.这使用了 ContextManager 这很好,因为它会在块退出时关闭该文件.此外,我使用模板来构建 url 的字符串.urlopen 不需要请求对象,只需要一个字符串.

Here's how I'd deal with the url building and downloading. I'm making sure to name the file as the basename of the url (the last bit after the trailing slash) and I'm also using the with clause for opening the file to write to. This uses a ContextManager which is nice because it will close that file when the block exits. In addition, I use a template to build the string for the url. urlopen doesn't need a request object, just a string.

import os
from urllib2 import urlopen, URLError, HTTPError


def dlfile(url):
    # Open the url
    try:
        f = urlopen(url)
        print "downloading " + url

        # Open our local file for writing
        with open(os.path.basename(url), "wb") as local_file:
            local_file.write(f.read())

    #handle errors
    except HTTPError, e:
        print "HTTP Error:", e.code, url
    except URLError, e:
        print "URL Error:", e.reason, url


def main():
    # Iterate over image ranges
    for index in range(150, 151):
        url = ("http://www.archive.org/download/"
               "Cory_Doctorow_Podcast_%d/"
               "Cory_Doctorow_Podcast_%d_64kb_mp3.zip" %
               (index, index))
        dlfile(url)

if __name__ == '__main__':
    main()

这篇关于如何使用 urllib2 在 python 中下载 zip 文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆