Pythonã®urllib2ãBeautifulSoupã使ã£ã¦ãYoutubeã®åç»ããã¦ã³ãã¼ãããã¹ã¯ãªãããä½ã£ãã
Youtubeã®åç»ãã¡ã¤ã«ã®URLãã©ãã«é ããã¦ããã®ãã解æããããã«ãYoutubeã®ã½ã¼ã¹ããã¼ã£ã¨è¦ç¶ãããã以ä¸ã®ã¹ã¯ãªããã§åå¾ã§ããã
ä½ã£ãå¾ã«ã°ã°ã£ãããPHPで行う動画変換(後編) (2/3):CodeZineã«è¼ã£ã¦ãâ¦ã
ã¾ãã¾ããè¯ããã
以ä¸ã®ã¹ã¯ãªããã
import re import simplejson import urllib2 import BeautifulSoup URLS = [ "http://www.youtube.com/", # ããã«ãã¦ã³ãã¼ããããYoutubeã®ãµã¤ãURLãæå® ] if __name__ == "__main__": p = re.compile("var swfConfig = ({.*?});") # ãã®ãã¿ã¼ã³ã§scriptããåå¾ def __get(url): bs = BeautifulSoup.BeautifulSoup(urllib2.urlopen(url).read()) f_name = bs.findAll("span", id="eow-title")[0].get("title") # Youtubeã®ãµã¤ãã«æ¸ãã¦ãtitleãåå¾ f_flv = "%s.flv" % f_name # åå¾ããã¿ã¤ãã«ããã¡ã¤ã«åã«ä½¿ç¨ for s in bs.findAll("script"): m = p.search(s.string) if s.string else None if m: j = simplejson.loads(m.group(1).replace("\\ ", "\\")) w = open(f_flv, "wb") w.write(urllib2.urlopen(j["args"]["fmt_url_map"].split("|")[-1]).read()) w.close() break for url in URLS: try: print "Begin: [%s]" % url __get(url) print "End : [%s]" % url except Exception, e: print "ERROR: [%s]\t%s" % (url, e)
éå§ãçµäºã®ããªã³ããã¦ãããã¨ã©ã¼ããç¥ããããç¨åº¦ã®å¶å¾¡æ¡å ãããã¦ãªãããã¾ãååããªã
以ä¸ã