import re

while True: url = input("请输入一个url:") match = re.match(r'(https?://)?([a-zA-Z0-9-]+.)+[a-zA-Z]{2,3}', url) if match: protocol = match.group(1)[:-3] if match.group(1) else 'http' domain = match.group(2)[:-1] print("协议:", protocol) print("域名:", domain) break else: print("输入错误,请重新输入")

写一个python3提示用户输入一个url解析出协议 http 或 https 和域名如果解析不出则报错要求重新输入直到能解析出来为止

原文地址: https://www.cveoy.top/t/topic/dWDK 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录