Extract all urls in a string with python3

后端 未结 5 1870
执念已碎
执念已碎 2020-12-22 08:46

I am trying to find a clean way to extract all urls in a text string.

After an extensive search, i have found many posts suggesting using regular expressions to do t

5条回答
  •  庸人自扰
    2020-12-22 09:24

    output = [x for x in input().split() if x.startswith('http://') or x.startswith('https://') or x.startswith('ftp://')]
    print(output)
    

    your example: http://ideone.com/wys57x

    After all you can also cut last character in elements of list if it is not a letter.

    EDIT:

    output = [x for x in input().split() if x.startswith('http://') or x.startswith('https://') or x.startswith('ftp://')]
    newOutput = []
    for link in output:
        copy = link
        while not copy[-1].isalpha():
            copy = copy[:-1]
        newOutput.append(copy)
    print(newOutput)
    

    Your example: http://ideone.com/gHRQ8w

提交回复
热议问题