Download file over HTTP using python | Download Mp3 over HTTP using python

Download file over HTTP using python .Hello guys, here is another useful article how do I download file over HTTP using python, Here in this article I am going to show and explain you how to download a file, MP3 and video from HTTP using python 2 and python 3.

Download file over HTTP using python | Download Mp3 over HTTP using python

Request is a  HTTP library in python with a lot of applications. To Download a file over HTTP we can use one of its application. First we have to install the request library in python

Using Request Library

How to install the request library in python

You can directly install using peep by typing the following command

pip install request

check this also
pip not recognized internal external command solved | how to install pip
>>> import requests
>>> url = "http://myexample.com/myfile.zip"
>>> r = requests.get(url)
>>> print len(r.content)

Downloading a Large file

r.content is  a string which is storing the file data. it is not possible to save all the data in a single string . To Solve this we have to make some changes in code:

r = requests.get(URL, stream = True)
stream = True
will cause the connection remains open. 
>>> import requests
>>> url = "http://myexample.com/myfile.zip"
>>> r = requests.get(url, stream = True)
>>> with open("Setting.pdf","wb") as pdf:
>>> for c in r.iter_content(c_size=1024):e
>>>if c:
>>>pdf.write(c)

using urllib Library

Python 3

Download file over HTTP using python
Download file over HTTP using python

urllib.request.urlopen

>>>import urllib.request
>>>response = urllib.request.urlopen('http://www.example.com/')
>>>html = response.read()

urllib.request.urlretrieve

>>>import urllib.request
>>>urllib.request.urlretrieve('http://www.example.com/songs/mp3.mp3', 'mp3.mp3')

Python 2

urllib2.urlopen

>>>import urllib2
>>>response = urllib2.urlopen('http://www.example.com/')
>>>html = response.read()
>>>urllib.urlretrieve (thanks PabloG)

import urllib
>>> urllib.urlretrieve('http://www.example.com/songs/mp3.mp3', 'mp3.mp3')

check this also

pip not recognized internal external command solved | how to install pip
AttributeError: ‘module’ object has no attribute ‘setdefaultencoding’
python is not recognized as an internal or external command | how to install python

final word

if you have any doubt or problem regarding  ‘Download file over HTTP using python | Download Mp3 over HTTP using python’ . Then you can feel free to ask the question. So, write your problem in the comment box. Because we are here to solve your all doubt.

reference:-  stackoverflow

Leave a Reply

Your email address will not be published. Required fields are marked *