本文介绍了如何使用 python 脚本下载 youtube 视频的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要使用 python 脚本从 youtube 下载视频.但是我无法从 youtube 页面获取视频的网址.

I need to download videos from youtube using a python script. However i am unable to get the url of the video from the youtube page.

例如,给定网址:http://www.youtube.com/watch?v=5qcmCUsw4EQ&feature=g-all-u&context=G2633db8FAAAAAAAAAAA

  1. 我需要将视频下载为 flv 或任何其他格式.此外,我还需要能够以多种质量下载它.
  2. 我尝试了几个脚本,如 youtube-dl 和 quvi,但它们都出现错误并且不起作用.请帮忙.将不胜感激.

推荐答案

您需要解析包含视频的 标签的 flashvars 变量.这些会发生变化,因此可能需要进行一些实验才能找到当前的变量名称.粗略地说,您需要使用像 mechanize 这样的库来抓取页面的 HTML,并使用 BeautifulSoup 来解析 HTML 并提取 flashvars> 元素的字段.然后查看变量以找出包含视频 URL 的变量.

You need to parse the flashvars variable of the <embed> tag that contains the video. These change around, so some experimentation may be required to find the current variable names. Roughly speaking, you'll want to use a libraries like mechanize to grab the HTML of the page and BeautifulSoup to parse the HTML and extract the flashvars field of the <embed> element. Then look around at the variables to figure out which one contains the video URL.

例如,

  br = mechanize.Browser()
  # Browser options
  br.set_handle_equiv(True)
  br.set_handle_redirect(True)
  br.set_handle_referer(True)
  br.set_handle_robots(False)
  # Follows refresh 0 but not hangs on refresh > 0
  br.set_handle_refresh(mechanize._http.HTTPRefreshProcessor(), max_time=1)
  # User-Agent (this is cheating, ok?)
  br.addheaders = [('User-agent', 'Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.0.1) Gecko/2008071615 Fedora/3.0.1-1.fc9 Firefox/3.0.1')]
  br.open('%s?v=%s' % (YOUTUBE_URL, vidId))
  soup = BeautifulSoup.BeautifulSoup(br.response().read())
  flashVars = urllib2.urlparse.parse_qs(soup.find('embed').get('flashvars'))
  # Return the first second video source URL
  return flashVars['fmt_stream_map'][0].split('|')[1]

这篇关于如何使用 python 脚本下载 youtube 视频的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 04:58