How to get the flv file from a youtube page [closed]
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 11 years ago.
Improve this questionHow would you get the "flv" file from A youtube page for example:www.youtube.com/watch?v=T8YCSJpF4g4... Any ideas?
So far ive tried:
import urllib
import os
import sys
page=urllib.urlopen('http://www.youtube.com/watch?v=FuLDIgfPae8')
result = page.read()
#result=open("test.html").read()
print result
startindex = re开发者_如何学JAVAsult.index("var swfArgs")
result = result[startindex:]
endindex=result.index("};")
result=result[:endindex]
result = result.split(",")
get={}
print result
def download(url,output):
"""Copy the contents of a file from a given URL to a local file."""
webFile = urllib.urlretrieve(url,output+".flv")
for line in result:
if "video_id" in line or "\"t\"" in line :
v,vnum = line.replace("\"","").split(":")
get[v.strip()]=vnum.strip()
dload = "http://www.youtube.com/get_video?video_id=%s&t=%s" % ( get['video_id'],get['t'])
try :
download(dload,get['video_id'])
except Exception,e:
print "Error: ",e
but this gives me an error:
startindex = result.index("var swfArgs")
ValueError: substring not found
and im not sure how to fix this error.
@icktoofay
how would i recreat all of these requests?:
Please help this is killing me.
The problem with your current code is that indeed, var swfArgs
does not appear in the source of that web page. YouTube is constantly changing their pages, so I'm going to tell you how to find what you'll need to do, rather than what YouTube currently does.
Open the page in a browser with good debugging tools. For example, Firefox with Firebug or Chrome with its developer tools. Open the developer tools and go to the network tab or whatever your tools call it.
Chrome's developer tools network tab:
Firebug's developer tools network tab:
Find the FLV. Find parameters in the URL and other related locations. See if any of that data is on the web page. Also see if any of that data is from other requests, which may have parameters which are on the web page.
Simply recreate the requests in Python, and you'll get the FLV, just as YouTube's official site gets it.
I suggest to take a look at youtube-dl. This is a short description taken from its website:
youtube-dl is a small command-line program to download videos from YouTube.com and a few more sites. It requires the Python interpreter, version 2.x (x being at least 5), and it is not platform specific. It should work in your Unix box, in Windows or in Mac OS X. It is released to the public domain, which means you can modify it, redistribute it or use it however you like.
精彩评论