python- youtube.获取网址视频列表 [英] python- youtube. Get url video list

查看:136
本文介绍了python- youtube.获取网址视频列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

示例列表

我写过(我是Python的新手):

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")

然后可行:

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?

推荐答案

简单的in应该可以:

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

这篇关于python- youtube.获取网址视频列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆