Just started in Python; wrote a script to get the names of all the titles in a public Youtube playlist given as input, but it got messier than it might have to be.
I looked around online and found HTMLParser, which I used to extract the titles, but it had problems with encoding which might have to do with there being foreign characters in the playlist HTML, so I messed around with encodes and decodes until it worked. But is there a prettier way to fix the problem?
import urllib.request
from html.parser import HTMLParser
playlistUrl = input("gib nem: ")
with urllib.request.urlopen(playlistUrl) as response:
playlist = response.read()
html = playlist.decode("utf-8").encode('cp1252','replace').decode('cp1252')
titles = ""
class MyHTMLParser(HTMLParser):
def handle_starttag(self, tag, attrs):
for attr in attrs:
if attr[0] == "data-title":
global titles
titles += attr[1] + "\n"
parser = MyHTMLParser()
parser.feed(html)
print(titles)
with open("playlistNames.txt", "w") as f:
f.write(titles)