Python equivalent of a given wget command

前端 未结 10 2193
面向向阳花
面向向阳花 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 10:55

    There is also a nice Python module named wget that is pretty easy to use. Found here.

    This demonstrates the simplicity of the design:

    >>> import wget
    >>> url = 'http://www.futurecrew.com/skaven/song_files/mp3/razorback.mp3'
    >>> filename = wget.download(url)
    100% [................................................] 3841532 / 3841532>
    >> filename
    'razorback.mp3'
    

    Enjoy.

    However, if wget doesn't work (I've had trouble with certain PDF files), try this solution.

    Edit: You can also use the out parameter to use a custom output directory instead of current working directory.

    >>> output_directory = 
    >>> filename = wget.download(url, out=output_directory)
    >>> filename
    'razorback.mp3'
    

提交回复
热议问题