YouTube批量下载脚本

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了YouTube批量下载脚本相关的知识,希望对你有一定的参考价值。

You
  1. #!/usr/bin/python
  2. import sgmllib
  3. import sys
  4.  
  5. class MyParser(sgmllib.SGMLParser):
  6. "A simple parser class."
  7.  
  8. def parse(self, s):
  9. "Parse the given string 's'."
  10. self.feed(s)
  11. self.close()
  12.  
  13. def __init__(self, verbose=0):
  14. "Initialise an object, passing 'verbose' to the superclass."
  15.  
  16. sgmllib.SGMLParser.__init__(self, verbose)
  17. self.hyperlinks = []
  18.  
  19. def start_a(self, attributes):
  20. "Process a hyperlink and its 'attributes'."
  21.  
  22. for name, value in attributes:
  23. if name == "href":
  24. if value.startswith("/watch?v="):
  25. self.hyperlinks.append("youtube-dl -t http://youtube.com"+value)
  26.  
  27. def get_hyperlinks(self):
  28. "Return the list of hyperlinks."
  29.  
  30. return self.hyperlinks
  31.  
  32. def f2(seq):
  33. # order preserving
  34. checked = []
  35. for e in seq:
  36. if e not in checked:
  37. checked.append(e)
  38. return checked
  39.  
  40. import urllib, sgmllib
  41.  
  42. # Get something to work with.
  43. f = urllib.urlopen(sys.argv[1])
  44. s = f.read()
  45.  
  46. # Try and process the page.
  47. # The class should have been defined first, remember.
  48. myparser = MyParser()
  49. myparser.parse(s)
  50.  
  51. # Get the hyperlinks.
  52. print f2(myparser.get_hyperlinks())
  53. yt = open("youtube_links","w")
  54.  
  55. for link in f2(myparser.get_hyperlinks()):
  56. yt.write(link)
  57. yt.write(" ")
  58.  
  59. print " YouTube Batch Download Script "youtube_links" has been generated. Execute it !"

以上是关于YouTube批量下载脚本的主要内容,如果未能解决你的问题,请参考以下文章

如何下载Youtube影片?需要甚么程式?

PHP Youtube视频下载脚本

Youtube视频下载脚本

结合两个代码片段?将用户输入的 Youtube url 转换为嵌入 url,然后将 iframe src 替换为转换后的 url

怎样下载youtube的字幕

如何修复空白片段?