Python Forum
how to just filter .mp4 in the text file - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: how to just filter .mp4 in the text file (/thread-25163.html)



how to just filter .mp4 in the text file - jacklee26 - Mar-22-2020

i have a test1111.txt, which have some html tag, and i wish to just filter .mp4, other word i don't want.
How to let it just print https://ttt111/tr/event/1234/1912/test1122.mp4



test1111.txt:
</div>
<div class="video-name " data-id="3" data-video="https://ttt111/tr/event/1234/1912/test1122.mp4" data-cover="https://ttt111/tr/event/123/19121/4_3_.png">
<div class="video-name__title">Chapter 4</div>
<div class="video-name__description">test</div>
</div>
<div class="video-name " data-id="4" data-video="https://ttt111/tr/event/1223/1912.mp4" data-cover="https://ttt111/tr/event/123/event/1122/1912/5_.png">
<div class="video-name__title">Chapter 5</div>
<div class="video-name__description">test</div>


searchfile = open("test111.txt", "r", encoding='utf-8')
for line in searchfile:
    if '.mp4' in line: 
        print (line)
searchfile.close()



RE: how to just filter .mp4 in the text file - buran - Mar-22-2020

why not use BeautifulSoup?
from bs4 import BeautifulSoup
file_name = 'test1111.txt'
with open(file_name) as f:
    soup = BeautifulSoup(f.read(),'html.parser')
    for div in soup.find_all('div', {'class':'video-name'}):
        print(div.get('data-video'))