Validating URLs in Python

前端 未结 5 672
广开言路
广开言路 2020-12-15 06:43

I\'ve been trying to figure out what the best way to validate a URL is (specifically in Python) but haven\'t really been able to find an answer. It seems like there isn\'t o

5条回答
  •  温柔的废话
    2020-12-15 07:20

    This looks like it might be a duplicate of How do you validate a URL with a regular expression in Python?

    You should be able to use the urlparse library described there.

    >>> from urllib.parse import urlparse # python2: from urlparse import urlparse
    >>> urlparse('actually not a url')
    ParseResult(scheme='', netloc='', path='actually not a url', params='', query='', fragment='')
    >>> urlparse('http://google.com')
    ParseResult(scheme='http', netloc='google.com', path='', params='', query='', fragment='')
    

    call urlparse on the string you want to check and then make sure that the ParseResult has attributes for scheme and netloc

提交回复
热议问题