Mar-07-2022, 05:14 AM
(This post was last modified: Mar-07-2022, 06:33 AM by Yoriz.
Edit Reason: Added code tags
)
I am trying to get some codes to be able to: enter the time frame (e.g. 2021-11-25 11:00 to 2021-11-29 02:19) -> find the links corresponding to those time stamps within the range -> download the .mp4 file under each link.
I have come up with some codes trying to download the mp4 files first, then I will fugure out how to download the files with specific time stamps, but I seemed to get stuck in the first step of just downloading mp4 files.
Here are the codes:
i
I have come up with some codes trying to download the mp4 files first, then I will fugure out how to download the files with specific time stamps, but I seemed to get stuck in the first step of just downloading mp4 files.
Here are the codes:
i
mport requests
from bs4 import BeautifulSoup
Video_url = "https://file-examples.com/index.php/sample-video-files/sample-mp4-files/"
def get_video_links():
# create response object
r = requests.get(Video_url)
# create beautiful-soup object
soup = BeautifulSoup(r.content,'html5lib')
# find all links on web-page
links = soup.findAll('a')
# filter the link ending with .mp4
video_links = [Video_url + link['href'] for link in links if link['href'].endswith('mp4')]
return video_links
def download_video_series(video_links):
for link in video_links:
'''iterate through all links in video_links
and download them one by one'''
# obtain filename by splitting url and getting
# last string
file_name = link.split('/')[-1]
print( "Downloading file:%s"%file_name)
# create response object
r = requests.get(link, stream = True)
# download started
with open(file_name, 'wb') as f:
for chunk in r.iter_content(chunk_size = 1024*1024):
if chunk:
f.write(chunk)
print( "%s downloaded!\n"%file_name )
print ("All videos downloaded!")
return
if __name__ == "__main__":
# getting all video links
video_links = get_video_links()
# download all videos
download_video_series(video_links)
