Manas Chaturvedi
Manas Chaturvedi

Reputation: 5540

Downloading a file over the Internet using Python

I am trying to use YouTubeinMp3's API to write a script which will download the music file on sending a request to a particular endpoint.

import requests
import json

si = {'video': 'https://www.youtube.com/watch?v=zTgLhIjiSas', 'autostart': 1}
res = requests.get("https://www.youtubeinmp3.com/download/", params = si)
print len(res.content)

Now, the resulting URL ie https://www.youtubeinmp3.com/download/?video=https://www.youtube.com/watch?v=zTgLhIjiSas&autostart=1 automatically starts downloading the corresponding mp3 file of the given video.

However, on executing the above script, the file does not start downloading. The status_code returned is 200. What seems to be wrong with my code?

Upvotes: 2

Views: 249

Answers (2)

Copy and Paste
Copy and Paste

Reputation: 526

from __future__ import unicode_literals
import youtube_dl

ydl_opts = {}
with youtube_dl.YoutubeDL(ydl_opts) as ydl:
    ydl.download(['http://www.youtube.com/watch?v=BaW_jenozKc'])

Before you run the above you'll need to install the youtube_dl package using pip.
Open up your terminal/cmd and type:

pip install youtube_dl

Upvotes: 1

kmaork
kmaork

Reputation: 6012

The generated URL leads to a HTML page, that redirects you to another page, that triggers a download. This URL is not a direct URL to the mp3 file itself. To get the mp3's URL you can scrape those pages.

Upvotes: 2

Related Questions