简体   繁体   English

Python:从单个URL下载多个.gz文件

[英]Python: Download multiple .gz files from single URL

I am having trouble downloading multiple network files from an online directory. 我无法从在线目录下载多个网络文件。 I am using a virtual Linux environment (Lubuntu) over VMware. 我在VMware上使用虚拟Linux环境(Lubuntu)。 My aim is to access a subfolder and download all the .gz files it contains into a new local directory that is different from the home directory. 我的目的是访问一个子文件夹并将其包含的所有.gz文件下载到与主目录不同的新本地目录中。 I tried multiple solutions and this is the closest I got. 我尝试了多种解决方案,这是我得到的最接近的解决方案。

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(100, 250,5):
        url = ("http://data.ris.ripe.net/rrc00/2016.01/updates20160128.0%d.gz"
                %(index))
        dlfile(url)

if __name__ == '__main__':
    main()

The online directory needs no authentication, a link can be found here . 在线目录无需身份验证,可以在此处找到链接。

I tried string manipulation and using a loop over the filenames, but it gave me the following error: 我尝试了字符串操作并在文件名上使用了循环,但这给了我以下错误:

HTTP Error: 404 http://data.ris.ripe.net/rrc00/2016.01/updates20160128.0245.gz

Look at the url 看网址

Good url: http://data.ris.ripe.net/rrc00/2016.01/updates.20160128.0245.gz 正确的网址: http://data.ris.ripe.net/rrc00/2016.01/updates.20160128.0245.gz : http://data.ris.ripe.net/rrc00/2016.01/updates.20160128.0245.gz

Bad url (your code): http://data.ris.ripe.net/rrc00/2016.01/updates20160128.0245.gz 网址错误(您的代码): http://data.ris.ripe.net/rrc00/2016.01/updates20160128.0245.gz : http://data.ris.ripe.net/rrc00/2016.01/updates20160128.0245.gz

A dot between updates and 2016 is missing 更新和2016年之间缺少一个点

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

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