Created
November 14, 2019 10:09
-
-
Save mehedithedue/92b07c0bd365b6cad4c84a664d1a065d to your computer and use it in GitHub Desktop.
Download all mp4 file from a url and save in destination folder by Python
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from bs4 import BeautifulSoup | |
import wget | |
import requests | |
import os | |
url = 'http://canvas.projekti.info/laracast/%20Intermediate%20Laravel/' | |
ext = 'mp4' | |
destinationFolder = "/home/mehedi/Downloads/intermediate_laravel/" | |
def getAllPaths(page, ext) : | |
fileUrl = [] | |
page = requests.get(url).text | |
soup = BeautifulSoup(page, 'html.parser') | |
for node in soup.find_all('a'): | |
if(node.get('href').endswith(ext)) : | |
fileUrl.append(node.get('href')) | |
return fileUrl | |
def downloadFile(downloadlink, fileName): | |
with requests.get(downloadlink, stream=True) as r: | |
r.raise_for_status() | |
filePath = os.path.join(destinationFolder, fileName) | |
print("Downloading to", os.path.abspath(filePath)) | |
with open(filePath, 'wb') as f: | |
for chunk in r.iter_content(chunk_size=8 * 1024): | |
if chunk: | |
f.write(chunk) | |
f.flush() | |
f.close() | |
result = getAllPaths(url, ext) | |
for fileName in result : | |
downloadFile(url+fileName, fileName) | |
print(fileName + " Downloaded") | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment