How To Create File Downloader Using Python - part 7

Posted by Suraj Singh on January 26, 2017 · 8 mins read
Hello readers,


                           This is our seventh part of web scraping tutorials. and in this tutorial, I am gonna to show you how to create file downloading program using python . but if you are new visitor then first check our index or For sixth Part Click Here

So, Let;s Talk About Today's Topic.

readers, Here We will try to create python script that can download file from given url address.
So, In Today's topic, we will learn about how to use urllib urlretrieve function in python.

So, Let's Start.

Here, I am Sharing My Demo Codes But If You Want More Better Example Then, You Can Modify these codes yourself or Download This Script From My GitHub repository (link given at the end of these codes ).

1. download.py


 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#!/usr/bin/python
# -*- coding: utf-8 -*-
#
#
# Suraj
# surajsinghbisht054@gmail.com
# www.bitforestinfo.com
#
# Import Modules
import urllib
import sys
import os

# Check Provided Arguments
if len(sys.argv)==1:
print "\n[*] Please Provide File Url Address:\n Usages: python {} www.examplesite.com\\path\\to\\file.zip\n".format(sys.argv[0])
sys.exit(0)

# Progress Reporter
def progress(blocks, block_size, total_block_size):
current_received_blocks = blocks * block_size # Calculate Total Block Received

percent = (100 * current_received_blocks/total_block_size) # Change in percentage

sys.stdout.write('\rPercent : {}% [{}]'.format(str(percent),str('#'*(percent/10)).ljust(10))) # Print Percentage with prograss bar
return

# File Name Creator
def filename(url):
# Return Url Address Base Name
url_base = os.path.basename(url)

if '.' in url_base: # If file extension available
base = url_base
print "[+] File Saved As : ",base
else:
base = raw_input("[*] File Saved As : ")
return base

# Download Function
def download(url, file_name):
print "\n"
urllib.urlretrieve(url, file_name, progress) # Downloading File
print "\n\n"
return

# Main Function For File Name Management
def main(url, fname=None):
if fname:
base = fname
else:
base = filename(url)

if base:
download(url, base)
return

if __name__ == '__main__':
main(sys.argv[1])

Warning I am Creating This Tutorial Only For Practise and Educational Purpose. I will not Take any type of responsibility about any illegal activities.

For Downloading, Raw Script Click Here


In Our Future Tutorials, we will try to create youtube video downloader, live score scraper, email harvester , Amazon product details scraper and many other scripts also. 

For More Update, Visit Our Regularly. 
And Subscribe Our Blog, 


Follow Us and share it.
For Any Type of Suggestion Or Help
Contact me:
Suraj
surajsinghbisht054@gmail.com