小编Jus*_*joe的帖子

如何使用urllib2在python中下载zip文件?

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

Q1 - 我如何更改下载zip mp3文件?Q2 - 将变量传递到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: …
Run Code Online (Sandbox Code Playgroud)

python urllib2

30
推荐指数
1
解决办法
5万
查看次数

标签 统计

python ×1

urllib2 ×1