python- youtube。获取网址视频列表

时间:2016-04-26 02:11:50

标签: python youtube beautifulsoup urllib2

我正在使用python 2.7。 我想用特定YouTube列表中的视频列表创建一个txt:

example list

我写道(我在Python中是全新的):

from bs4 import BeautifulSoup
import urllib2
import re 


url='https://www.youtube.com/playlist?list=PLYjSYQBFeM-zQeZFpWeZ_4tnhc3GQWNj8'
page=urllib2.urlopen(url)
soup = BeautifulSoup(page.read())

href_tags = soup.find_all(href=True)

ff = open("C:/exp/file.txt", "w")

然后这有效:

for i in href_tags:
    ff.write(str(i))
ff.close()

但是,由于我只想保留那些内部“观察”的东西,我试着改为:

for i in href_tags:
    if re.findall('watch',str(i))=='watch':
        ff.write(str(i))
ff.close()

但我有一个空的txt。

如何只保留链接?有更好的方法吗?

3 个答案:

答案 0 :(得分:2)

# This code will work if you're are willing to use a newer version of Python
from bs4 import BeautifulSoup
import requests

class Playlist():
    def __init__(self, playListUrl):
        self._playListUrl = playListUrl

        # This will take the html text from Youtube playList url and stores it in a variable called html-doc.
        self._htmldoc = requests.get(str(self._playListUrl)).text
        self._soup = BeautifulSoup(self._htmldoc, 'html.parser')

        # This will create a list of all the titles and the youtube url videos using the html-doc.
        self._rawList = self._soup('a', {'class': 'pl-video-title-link'})

        # This will loop through a list of titles and Youtube urls and formats it nicely for you.
        for link in self._rawList:
            print('{0}'.format(link.string) + 'http://youtube.com' + '{0}'.format(link.get('href')))

# To use this class all you got to do is:
# 1 - Create a new object to use the class..
# 2- put a youtube playlist url where it is shown below..
# 3- Run it, and enjoy.
objPlaylist = Playlist('put Youtube playlist url here')

答案 1 :(得分:0)

一个简单的in应该:

for i in href_tags:
    if 'watch' in str(i):
        ff.write(str(i))
    ff.close()

答案 2 :(得分:0)

此外,您可以将youtube-dl与软件包subprocess一起安装

youtube-dl https://www.youtube.com/playlist?list=PLrhzvIcii6GNjpARdnO4ueTUAVR9eMBpc --yes-playlist --get-title

youtube-dl https://www.youtube.com/playlist?list=PLrhzvIcii6GNjpARdnO4ueTUAVR9eMBpc --yes-playlist --get-url