python- youtube. Get url video list

谁都会走 提交于 2019-12-13 06:13:03

问题


I am working with python 2.7. I want to create a txt with the list of videos in a particular youtube list:

example list

I wrote (I'm totally new in 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")

and then this worked:

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

But, since I want to keep only those that have "watch" inside, I tried instead:

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

But I got an empty txt.

How can I keep only the links? Is there a better way to do this?


回答1:


A simple in should do:

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



回答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')



回答3:


Also, you can install youtube-dl with package 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


来源:https://stackoverflow.com/questions/36854168/python-youtube-get-url-video-list

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!