파이썬

python html tag 제거 경로 금지 문자 제거

by nanumi posted Oct 26, 2021
?

단축키

Prev이전 문서

Next다음 문서

ESC닫기

크게 작게 위로 아래로 댓글로 가기 인쇄

python html tag 제거 경로 금지 문자 제거

 

 

from pytube import YouTube
 
# 경로 금지 문자 제거, HTML문자 제거
import re
def filename_remover(string):
        cleaner = re.compile('<.*?>|&([a-z0-9]+|#[0-9]{1,6}|#x[0-9a-f]{1,6});') #<tag>, &nbsp 등등 제거
        string = re.sub(cleaner, '', string)
        while(string[-1] == '.'):
            string = string[:-1] #끝에 . 제거 ex) test... -> test
        non_directory_letter = ['/', ':', '*', '?', '<', '>', '|'] #경로 금지 문자열 제거
        for str_ in non_directory_letter:
                if str_ in string:
                        string = string.replace(str_, "")
        return string
 
url = 'https://www.youtube.com/watch?v=FbVeMmO6G60&t=1s'
yt = YouTube(url)
 
caption = yt.captions.all()[0]  # 첫번째 자막 선택
caption_xml=caption.xml_captions # html문서로 변환
re_string=filename_remover(caption_xml)
print(re_string)