Python equivalent of a given wget command

前端 未结 10 2224
面向向阳花
面向向阳花 2020-12-02 10:04

I\'m trying to create a Python function that does the same thing as this wget command:

wget -c --read-timeout=5 --tries=0 \"$URL\"

-c

10条回答
  •  醉酒成梦
    2020-12-02 11:08

    Let me Improve a example with threads in case you want download many files.

    import math
    import random
    import threading
    
    import requests
    from clint.textui import progress
    
    # You must define a proxy list
    # I suggests https://free-proxy-list.net/
    proxies = {
        0: {'http': 'http://34.208.47.183:80'},
        1: {'http': 'http://40.69.191.149:3128'},
        2: {'http': 'http://104.154.205.214:1080'},
        3: {'http': 'http://52.11.190.64:3128'}
    }
    
    
    # you must define the list for files do you want download
    videos = [
        "https://i.stack.imgur.com/g2BHi.jpg",
        "https://i.stack.imgur.com/NURaP.jpg"
    ]
    
    downloaderses = list()
    
    
    def downloaders(video, selected_proxy):
        print("Downloading file named {} by proxy {}...".format(video, selected_proxy))
        r = requests.get(video, stream=True, proxies=selected_proxy)
        nombre_video = video.split("/")[3]
        with open(nombre_video, 'wb') as f:
            total_length = int(r.headers.get('content-length'))
            for chunk in progress.bar(r.iter_content(chunk_size=1024), expected_size=(total_length / 1024) + 1):
                if chunk:
                    f.write(chunk)
                    f.flush()
    
    
    for video in videos:
        selected_proxy = proxies[math.floor(random.random() * len(proxies))]
        t = threading.Thread(target=downloaders, args=(video, selected_proxy))
        downloaderses.append(t)
    
    for _downloaders in downloaderses:
        _downloaders.start()
    

提交回复
热议问题