티스토리 뷰

공부

[Python] regexp url

승가비 2020. 5. 31. 12:01
728x90
import re
regex = re.compile(
        r'^(?:http|ftp)s?://' # http:// or https://
        r'(?:(?:[A-Z0-9](?:[A-Z0-9-]{0,61}[A-Z0-9])?\.)+(?:[A-Z]{2,6}\.?|[A-Z0-9-]{2,}\.?)|' #domain...
        r'localhost|' #localhost...
        r'\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})' # ...or ip
        r'(?::\d+)?' # optional port
        r'(?:/?|[/?]\S+)$', re.IGNORECASE)

print(re.match(regex, "http://www.example.com") is not None) # True
print(re.match(regex, "example.com") is not None)            # False

https://stackoverflow.com/questions/7160737/python-how-to-validate-a-url-in-python-malformed-or-not

 

Python - How to validate a url in python ? (Malformed or not)

I have url from the user and I have to reply with the fetched HTML. How can I check for the URL to be malformed or not ? For Example : url='google' // Malformed url='google.com' // Malformed u...

stackoverflow.com

 

728x90
댓글