在Python中验证URL [英] Validating URLs in Python

查看:210
本文介绍了在Python中验证URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试找出验证URL的最佳方法是什么(特别是在Python中),但实际上并没有找到答案.似乎没有一种已知的验证URL的方法,它取决于您认为可能需要验证哪些URL.同样,我发现很难找到一个易于阅读的URL结构标准.我确实找到了RFC 3986和3987,但它们包含的内容远不止其结构.

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 one known way to validate a URL, and it depends on what URLs you think you may need to validate. As well, I found it difficult to find an easy to read standard for URL structure. I did find the RFCs 3986 and 3987, but they contain much more than just how it is structured.

我是否缺少某些内容,或者没有一种标准的方法可以验证URL?

Am I missing something, or is there no one standard way to validate a URL?

推荐答案

这可能与您应该能够使用此处描述的urlparse库.

You should be able to use the urlparse library described there.

>>> from urllib.parse import urlparse # python2: from urlparse import urlparse
>>> urlparse('actually not a url')
ParseResult(scheme='', netloc='', path='actually not a url', params='', query='', fragment='')
>>> urlparse('http://google.com')
ParseResult(scheme='http', netloc='google.com', path='', params='', query='', fragment='')

在要检查的字符串上调用urlparse,然后确保ParseResult具有schemenetloc

call urlparse on the string you want to check and then make sure that the ParseResult has attributes for scheme and netloc

这篇关于在Python中验证URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆