I am working with python 2.7. I want to create a txt with the list of videos in a particular youtube 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?
A simple in
should do:
for i in href_tags:
if 'watch' in str(i):
ff.write(str(i))
ff.close()