Python - HTTP Data Download

Neha Kumawat

a year ago

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 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 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 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 −





   


Submit Review

We're Online!

Chat now for any query