Con*_*CPP 1 python youtube video
我需要使用python脚本从youtube下载视频.但是,我无法从youtube页面获取视频的网址.
例如,给定网址:http://www.youtube.com/watch?v = 5qcmCUsw4EQ&feature = g-all-u&context = G2633db8FAAAAAAAAAAAA
jam*_*nvc 10
您需要解析包含视频flashvars的<embed>标记的变量.这些变化,因此可能需要一些实验来查找当前变量名称.粗略地说,您将希望使用类似于mechanize抓取页面BeautifulSoup的HTML并解析HTML并提取元素flashvars字段的库<embed>.然后查看变量以确定哪个变量包含视频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]
Run Code Online (Sandbox Code Playgroud)