【发布时间】:2016-04-26 02:11:50
【问题描述】:
我正在使用 python 2.7。 我想用特定 youtube 列表中的视频列表创建一个 txt:
我写的(我是 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。
我怎样才能只保留链接?有没有更好的方法来做到这一点?
【问题讨论】:
标签: python youtube beautifulsoup urllib2