Search code examples
pythonpython-2.7urllib2

How can i download a certain file into a specific folder using python?


assuming my link is : web_link = 'https://link' and my destination file that i want the content to be downloaded in is path. How can i download the content of web_link in path knowing that i'm using python2.7.


Solution

  • You can download using urllib:-

    Python 2

    import urllib 
    urllib.urlretrieve("web_link", "path")   
    

    you can use requests if weblink needs authentication :

    import requests
    r = requests.get(web_link, auth=('usrname', 'password'), verify=False,stream=True)   #Note web_link is https://
    r.raw.decode_content = True
    with open("path", 'wb') as f:
        shutil.copyfileobj(r.raw, f)    
    

    Python 3

    import urllib.request
    urllib.request.urlretrieve("web_link", "path")