Home > Blockchain >  EASY PYTHON SELENIUM: How do I download an mp4 WITHOUT using urllib?
EASY PYTHON SELENIUM: How do I download an mp4 WITHOUT using urllib?

Time:02-27

I'm trying to download this video: https://www.learningcontainer.com/wp-content/uploads/2020/05/sample-mp4-file.mp4

I tried the following but it doesn't work.

link = "https://www.learningcontainer.com/wp-content/uploads/2020/05/sample-mp4-file.mp4"
urllib.request.urlretrieve(link, 'video.mp4')

I'm getting:

urllib.error.HTTPError: HTTP Error 403: Forbidden

Is there another way to download an mp4 file without using urllib?

CodePudding user response:

I have no problem to download with module requests

import requests

url = 'https://www.learningcontainer.com/wp-content/uploads/2020/05/sample-mp4-file.mp4'

response = requests.get(url)

with open('video.mp4', 'wb') as f:  # use `"b"` to open in `bytes mode`
    f.write(response.content)       # use `.content` to get `bytes`

It was small file ~10MB but for bigger file you may download in chunks.

import requests

url = 'https://www.learningcontainer.com/wp-content/uploads/2020/05/sample-mp4-file.mp4'

response = requests.get(url, stream=True)

with open('video.mp4', 'wb') as f:
    for chunk in response.iter_content(10000):  # 10_000 bytes
        if chunk:
            #print('.', end='')  # every dot will mean 10_000 bytes 
            f.write(chunk)

Documentation shows Streaming Requests but for text data.


url is a string so you can use string-functions to get element after last /

filename =  url.split('/')[-1]

Or you can try to use os.path

At least it works on Linux - maybe because Linux also use / in local paths.

import os

head, tail = os.path.split(url)

# head: 'https://www.learningcontainer.com/wp-content/uploads/2020/05'
# tail: 'sample-mp4-file.mp4'
  • Related