首页 > 解决方案 > 如何使用python从文本文件中获取链接

问题描述

所以我有一个看起来像这样的文本文件

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

我想以可点击的格式打印以“.mp4”结尾的链接。我怎么能用python做到这一点?

标签: pythontext

解决方案


也许使用正则表达式库

   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)

在这里,您将过滤结果以仅获取以“https://”开头并以“.mp4”结尾的字符串,在获取字符串后,从“word”中删除创建的字符串并再次运行程序直到出现不匹配。


推荐阅读