Ahmet Bilgin
Ahmet Bilgin

Reputation: 33

How to get links from text file using python

So i have a text file that looks like this

randomthings inside text file "https://linkforvideo.mp4" a lot more random things "https://linkforphoto.jpg"

i want to print the links that ends with ".mp4" in a clickable format. How can i do this using python?

Upvotes: 1

Views: 320

Answers (1)

Parker
Parker

Reputation: 342

Maybe using the Regular expresion library

   import re 
    word = 'randomthings inside text file "https://linkforvideo.mp4" a lot more random things "https://linkforphoto.jpg"'
    link=[]
    result = re.search('https://(.*?).mp4', word)
    while True:
        try:
            result_string = result.group(0)
            link.append(result_string)
            word= word.replace(result_string, "")
            result = re.search('https://(.*?).mp4', word)
        except : break
    print(link)

Here you are filtering the result to only get the strings that starts with "https://" and ends with ".mp4", after you get the string, you delete the founded string from "word" and run the program again until there is no match.

Upvotes: 1

Related Questions