python中无效的URL解析器 [英] Invalid URL parser in python
问题描述
[是的,标题不是错字!]
在 python 中,我需要解析 URL 的东西.我不敢相信标准还不存在.由于 URL 是在配置中设置的,我想确保它不是垃圾.
有 urlparse.urlparse,但它只解析有效 URL"(一些无效 URL 有时会引发未记录的 ValueError)
例如
<预><代码>>>>导入 urlparse>>>urlparse.urlparse('http://aa :: aa !aa:11.com:aa').netloc'啊::啊!aa:11.com:aa'展示了 urlparse 如何解析我认为无效的 URL.
URL 解析和 URL 验证实际上是不同的任务.
urlparse.urlparse
进行解析,验证通常使用正则表达式机器(Python 内置 re
模块).
以下是来自 Django 框架的 URL 验证示例:
regex = re.compile(r'^(?:http|ftp)s?://' # http://或 https://r'(?:(?:[A-Z0-9](?:[A-Z0-9-]{0,61}[A-Z0-9])?\.)+(?:[AZ]{2,6}\.?|[A-Z0-9-]{2,}\.?)|' #domain...r'本地主机|'#本地主机...r'\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})' # ...或ipr'(?::\d+)?'# 可选端口r'(?:/?|[/?]\S+)$', re.IGNORECASE)
[Yes, the title is not a typo!]
In python I need something that parses a URL. I cannot believe something standard does not already exist. As the URL is set in a config I'd like to make sure it is not garbage.
There's urlparse.urlparse, but that only parses 'valid URLs' (with some invalid URLs sometimes raising an undocumented ValueError)
e.g.
>>> import urlparse
>>> urlparse.urlparse('http://aa :: aa ! aa:11.com:aa').netloc
'aa :: aa ! aa:11.com:aa'
shows how urlparse parses what I would consider an invalid URL.
URL parsing and URL validation are actually different tasks.
urlparse.urlparse
makes parsing, validation is usually made using regular expressions machine (built-in re
module in Python).
Here's and example of URL validation from Django framework:
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)
这篇关于python中无效的URL解析器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!