Py学习  »  Python

python 校验 URL 是否合法

爱情的枪 • 10 年前 • 6775 次点击  

以下是django的实现(正则表达式)

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)

具体实现时:

# utils.py
def is_http_url(s):
    """
    Returns true if s is valid http url, else false
    Arguments:
    - `s`:
    """
    if regex.match(s):
        return True
    else:
        return False

如果你认为没有http也是可以的,也可以简单如下:

#
# url
#
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 ipv4
    r'\[?[A-F0-9]*:[A-F0-9:]+\]?)'  # ...or ipv6
    r'(?::\d+)?'  # optional port
    r'(?:/?|[/?]\S+)$', re.IGNORECASE)

可参照 http://stackoverflow.com/questions/7160737/python-how-to-validate-a-url-in-python-malformed-or-not

Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/832
 
6775 次点击  
文章 [ 3 ]  |  最新文章 10 年前
Py站长
Reply   •   1 楼
Py站长    10 年前

Py站长
Reply   •   2 楼
Py站长    10 年前

爱情的枪
Reply   •   3 楼
爱情的枪    10 年前