python脚本,下载html内容而不是流式视频
问题描述:
我对python(和编程的东西)很陌生,所以我试图练习不同类型的练习。其中之一是使用脚本(python3)下载流式视频。问题是,我不下载视频,但网页的HTML内容...任何人都可以帮助我了解什么是错的?python脚本,下载html内容而不是流式视频
这里是我的代码:
import Mail
from urllib.request import Request, urlopen
import urllib
import time
import requests
mail = Mail.Mail()
def download_file(url):
local_filename = url.split('/')[-1]
# NOTE the stream=True parameter
r = requests.get(url, stream=True)
with open(local_filename, 'wb') as f:
for chunk in r.iter_content(chunk_size=1024):
if chunk: # filter out keep-alive new chunks
f.write(chunk)
# f.flush() commented by recommendation from J.F.Sebastian
return local_filename
if __name__ == '__main__':
url = 'https://youtu.be/xKsEKgAF7kE'
filename = download_file(url)
print(filename, " has been downloaded.")
不注重豆腐渣样的邮件的事情,这是我再使用,或无用的进口创造了一个类:我第一次在第一次看到不同的,我没使用请求库,我的代码是不同的,但结果是相同的(这段代码来自其他stackoverflow主题)。
非常感谢您的帮助! :)
答
**最好使用pytube模块并查看其内部。所以我可能会帮助你**
from pytube import YouTube
from pprint import pprint
yt = YouTube("http://www.youtube.com/watch?v=Ik-RsDGPI5Y")
print(yt.get_videos())
# The filename is automatically generated based on the video title. You
# can override this by manually setting the filename.
# view the auto generated filename:
print(yt.filename)
# Pulp Fiction - Dancing Scene [HD]
# set the filename:
yt.set_filename('Enything')
# You can also filter the criteria by filetype.
print(yt.filter('flv'))
print(yt.filter(resolution='480p'))
video = yt.get('mp4', '720p')
# NOTE: get() can only be used if and only if one object matches your criteria.
# for example:
print(yt.videos)
#[<Video: MPEG-4 Visual (.3gp) - 144p>,
# <Video: MPEG-4 Visual (.3gp) - 240p>,
# <Video: Sorenson H.263 (.flv) - 240p>,
# <Video: H.264 (.flv) - 360p>,
# <Video: H.264 (.flv) - 480p>,
# <Video: H.264 (.mp4) - 360p>,
# <Video: H.264 (.mp4) - 720p>,
# <Video: VP8 (.webm) - 360p>,
# <Video: VP8 (.webm) - 480p>]
# Since we have two H.264 (.mp4) available to us... now if we try to call get()
# on mp4...
video = yt.get('mp4')
# MultipleObjectsReturned: 2 videos met criteria.
# In this case, we'll need to specify both the codec (mp4) and resolution
# (either 360p or 720p).
# Okay, let's download it! (a destination directory is required)
video.download('/tmp/')
你应该看看YouTube DL是一个Python项目来做这件事。它也适用于其他不是YouTube的网站。 https://github.com/rg3/youtube-dl –
另外一个侧面说明。我认为你正在通过刮擦破坏YouTubes的服务条款。他们有一个他们希望使用的API。 –
感谢您的评论,我知道这些工具已经存在,这是实用的培训练习。我稍后会尝试自动下载视频(例如,我最喜欢的一集的新剧集将被重新发布)。这里,该示例是一个YouTube网址,但它可能是其他内容。 – Florian