且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在Python中验证URL

更新时间:2023-02-26 12:43:30

这可能与您应该能够使用此处描述的urlparse库.

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='')

在要检查的字符串上调用urlparse,然后确保ParseResult具有schemenetloc

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