文章内容

2021/5/24 17:54:46,作 者: 黄兵

Python检查链接是否正确

下面是使用Python正则表达式验证链接是否正确:

def verify_crawler_common_attribute(crawler_uri, icon):
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)
if re.match(regex, icon) is None and re.match(regex, crawler_uri) is None:
return False

在验证之前需要去掉链接前后空格。

分享到:

发表评论

评论列表

user-ico

das on 回复 有用(0

da


user-ico

das on 回复 有用(0

da