我想下载一些视频,然而,迭代链接并将它们传递到open()
功能只会下载第一个视频,然后引发OSError
。
以下是打印出的内容
Downloading 1-Video1
[Downloading] 100% |###########################################| Time:
0:03:16
[1-Introduction] Successfully Downloaded
with open(str(file_name) + '.mp4', "wb") as f:
OSError: [Errno 22] Invalid argument: "2-Video2.mp4"
以下是我的代码片段
# iterate over a dict containing k as the stage name and v as a list of
# all the videos in that stage
for k, v in stages.items():
make_dir(k)
os.chdir(k)
for i in v:
# i[0] is the title of the video, i[1] is the video link
video_download(i[0], i[1])
def video_download(file_name, video_link):
with open(file_name + '.mp4', "wb") as f:
URL = requests.get(video_link) # , auth=(EMAIL, PASSWORD)
soup = BeautifulSoup(URL.text, 'lxml')
video_mp4_link = soup.find('source', {'type': 'video/mp4'})['src']
print('\n' + "Downloading %s" % file_name)
response = requests.get(
video_mp4_link, stream=True)
total_length = response.headers.get('content-length')
# check if there is a content lenght
if total_length is None:
f.write(response.content)
else:
# downloading the video with a progress bar
dl = 0
total_length = int(total_length)
widgets = ['[Downloading] ', Percentage(), ' ',
Bar(marker='#', left='|', right='|'), ' ', ETA(), ' ']
pbar = ProgressBar(widgets=widgets, maxval=100)
pbar.start()
for data in response.iter_content(chunk_size=8192):
dl += len(data)
f.write(data)
pbar.update((dl / total_length) * 100)
pbar.finish()
print("[%s] Successfully Downloaded" % file_name)
# I am going to use it a lot that's why I created the following func
def make_dir(directory):
try:
os.makedirs(directory)
except OSError as e:
if e.errno != errno.EEXIST:
raise