Validating URLs in Python

前端 未结 5 669
广开言路
广开言路 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

    you can also try using urllib.request to validate by passing the URL in the urlopen function and catching the exception for URLError.

    from urllib.request import urlopen, URLError
    
    def validate_web_url(url="http://google"):
        try:
            urlopen(url)
            return True
        except URLError:
            return False
    

    This would return False in this case

提交回复
热议问题