All Courses

Python - HTTP Data Download

Neha Kumawat

10 months ago

Python HTTP data Download
Table of Content
  • Download data from a serer using python's module
  • Fetching the Files
  • Reading the Data
            We can download data from a serer using python's module which handle ftp or File Transfer Protocol. We can also read the data and later save it to the local system.
We need to install the module ftplib to acheive this. pip install ftplib Fetching the Files We can fetch a specific file by using the get file method. This method moves a copy of the file from the remote system to the local system from where the ftp connection was initiated. import ftplib import sys def getFile(ftp, filename): try: ftp.retrbinary("RETR " + filename ,open(filename, 'wb').write) except: print "Error" ftp = ftplib.FTP("ftp.nluug.nl") ftp.login("anonymous", "ftplib-example-1") ftp.cwd('/pub/') change directory to /pub/ getFile(ftp,'README.nluug') ftp.quit() When we run the above program, we find the file README.nlug to be present in the local system from where the connection was initiated. Reading the Data In the below example we use the module urllib2 to read the required portion of the data which we can copy and save it to local system. When we run the above program, we get the following output − import urllib2 response = urllib2.urlopen('http://www.tutorialspoint.com/python') html = response.read(200) print html When we run the above program, we get the following output −
pip install ftplib Fetching the Files We can fetch a specific file by using the getfile method. This method moves a copy of the file from the remote system to the local system from where the ftp connection was initiated. import ftplib import sys def getFile(ftp, filename): try: ftp.retrbinary("RETR " + filename ,open(filename, 'wb').write) except: print "Error" ftp = ftplib.FTP("ftp.nluug.nl") ftp.login("anonymous", "ftplib-example-1") ftp.cwd('/pub/') change directory to /pub/ getFile(ftp,'README.nluug') ftp.quit() When we run the above program, we find the file README.nlug to be present in the local system from where the connection was initiated. Reading the Data In the below example we use the module urllib2 to read the required portion of the data which we can copy and save it to local system. When we run the above program, we get the following output − import urllib2 response = urllib2.urlopen('http://www.tutorialspoint.com/python') html = response.read(200) print html When we run the above program, we get the following output −

pip install ftplib

Fetching the Files

       We can fetch a specific file by using the get file method. This method moves a copy of the file from the remote system to the local system from where the ftp connection was initiated.

import ftplib
import sys
 
def getFile(ftp, filename):
    try:
        ftp.retrbinary("RETR " + filename ,open(filename, 'wb').write)
    except:
        print "Error"
 
 
ftp = ftplib.FTP("ftp.nluug.nl")
ftp.login("anonymous", "ftplib-example-1")
 
ftp.cwd('/pub/')          change directory to /pub/
getFile(ftp,'README.nluug')
 
ftp.quit()

When we run the above program, we find the file README.nlug to be present in the local system from where the connection was initiated.

Reading the Data

     In the below example we use the module urllib2 to read the required portion of the data which we can copy and save it to local system.
When we run the above program, we get the following output −

import urllib2
response = urllib2.urlopen('http://www.tutorialspoint.com/python')
html = response.read(200)
print html

When we run the above program, we get the following output −





   


Liked what you read? Then don’t break the spree. Visit our insideAIML blog page to read more awesome articles. 
Or if you are into videos, then we have an amazing Youtube channel as well. Visit our InsideAIML Youtube Page to learn all about Artificial Intelligence, Deep Learning, Data Science and Machine Learning. 
Keep Learning. Keep Growing.

Submit Review