How do you extract a url from a string using python?

后端 未结 5 1823
庸人自扰
庸人自扰 2020-11-27 05:33

For example:

string = \"This is a link http://www.google.com\"

How could I extract \'http://www.google.com\' ?

(Each link will be

5条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-11-27 06:07

    There is another way how to extract URLs from text easily. You can use urlextract to do it for you, just install it via pip:

    pip install urlextract
    

    and then you can use it like this:

    from urlextract import URLExtract
    
    extractor = URLExtract()
    urls = extractor.find_urls("Let's have URL stackoverflow.com as an example.")
    print(urls) # prints: ['stackoverflow.com']
    

    You can find more info on my github page: https://github.com/lipoja/URLExtract

    NOTE: It downloads a list of TLDs from iana.org to keep you up to date. But if the program does not have internet access then it's not for you.

提交回复
热议问题