import requests def extract_video_links(url): """ 提取视频链接 """ response = requests.get(url) html = response.text # 在HTML中查找视频链接 pattern = r'src="([^"]+\.mp4)"' matches = re.findall(pattern, html) return matches def download_video(url, filename): """ 下载视频 """ response = requests.get(url, stream=True) with open(filename, 'wb') as file: for chunk in response.iter_content(chunk_size=1024): if chunk: file.write(chunk) # 提取视频链接 url = "https://example.com/video.html" links = extract_video_links(url) # 下载视频 for link in links: filename = link.split("/")[-1] download_video(link, filename) # 在上面的示例代码中,`extract_video_links()`函数使用`requests`库发送HTTP请求,并从HTML中查找视频链接。 # `download_video()`函数使用`requests`库下载视频,并将其保存到本地文件。 # 最后,使用循环遍历所有链接,并调用`download_video()`函数下载每个视频。请注意,示例代码仅作为参考,实际应用中需要根据具体情况进行修改。
标签:视频,links,Python,chunk,url,MP4,video,requests From: https://www.cnblogs.com/kaixin2018/p/17558334.html