Skip to content

Commit c2395a5

Browse files
Merge pull request geekcomputers#339 from akshaykumar123/patch-1
Create pythonVideoDownloader.py
2 parents 9ff94ee + 86a1092 commit c2395a5

File tree

1 file changed

+66
-0
lines changed

1 file changed

+66
-0
lines changed

pythonVideoDownloader.py

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
import requests
2+
from bs4 import BeautifulSoup
3+
4+
'''
5+
URL of the archive web-page which provides link to
6+
all video lectures. It would have been tiring to
7+
download each video manually.
8+
In this example, we first crawl the webpage to extract
9+
all the links and then download videos.
10+
'''
11+
12+
# specify the URL of the archive here
13+
archive_url = "http://www-personal.umich.edu/~csev/books/py4inf/media/"
14+
15+
def get_video_links():
16+
17+
# create response object
18+
r = requests.get(archive_url)
19+
20+
# create beautiful-soup object
21+
soup = BeautifulSoup(r.content,'html5lib')
22+
23+
# find all links on web-page
24+
links = soup.findAll('a')
25+
26+
# filter the link sending with .mp4
27+
video_links = [archive_url + link['href'] for link in links if link['href'].endswith('mp4')]
28+
29+
return video_links
30+
31+
32+
def download_video_series(video_links):
33+
34+
for link in video_links:
35+
36+
'''iterate through all links in video_links
37+
and download them one by one'''
38+
39+
# obtain filename by splitting url and getting
40+
# last string
41+
file_name = link.split('/')[-1]
42+
43+
print "Downloading the file:%s"%file_name
44+
45+
# create response object
46+
r = requests.get(link, stream = True)
47+
48+
# download started
49+
with open(file_name, 'wb') as f:
50+
for chunk in r.iter_content(chunk_size = 1024*1024):
51+
if chunk:
52+
f.write(chunk)
53+
54+
print "%s downloaded!\n"%file_name
55+
56+
print "All videos are downloaded!"
57+
return
58+
59+
60+
if __name__ == "__main__":
61+
62+
# getting all video links
63+
video_links = get_video_links()
64+
65+
# download all videos
66+
download_video_series(video_links)

0 commit comments

Comments
 (0)