Python 中是否有预定义的 URL 类?
当然,我可以自己编写这样的类,但在开始重新发明轮子之前,我想先看看周围。
我确实查看了 urllib2 和 urlparse。 urlparse 基本上具有我需要的功能,但它没有将其封装到像 java.net.URL 这样的类中。关于我对程序的分析,它的工作原理是颠倒的。
我还查看了 SplitResult
和 ParseResult
类中 urlparse
的源代码。它们具有一些基本功能,可用于子类化。但我必须将其余的 urlparse 函数重写为子类方法。
我还发现了 mxURL - Python 的灵活 URL 数据类型。它非常接近我真正想要的。只是这对于我的目的来说似乎有点过分了。
谁能建议另一种选择?我应该继续重新发明轮子吗?
我的解决方案:
为了获取 URL 类,我基本上做了两件事:
- 从
urlparse.ResultMixin
继承。 - 定义仅调用 urlparse.urlparse() 并将结果转换为 URL实例的参数。
I am looking for something like java.net.URL in python-modules, Django, Zope or wherever in Python.
I want it preferably from the semantics reason, because the result of analysis of concerned program implies that the URL plays an essential role in it. The consequence is that such URL class also will have great practical usage in that program.
Of course I could write such class on my own, but I'd like to look around before I start to reinvent the wheel.
I did look at urllib2 and urlparse. The urlparse
basically has the functionality I need, but it doesn't encapsulate it into a class like java.net.URL
. Regarding my analysis of my program it works upside-down.
I looked also into the source code of urlparse
at the classes SplitResult
and ParseResult
. They have some basic functionality and they can be used for subclassing. But I'll have to rewrite rest of the urlparse functions as the subclass methods.
I found also mxURL - Flexible URL Datatype for Python. It is very close to what I really want. Only it seems to be quite an overkill for my purpose.
Can anyone suggest another option? Should I proceed with reinventing the wheel?
My solution:
To get my URL class I did basically two things:
- Inherit from
urlparse.ResultMixin
. - Define function which only calls
urlparse.urlparse()
and transforms results to
parameters of URL instance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
urlparse
确实将 URL 封装到一个名为ParseResult
的类中,因此它可以被视为这些 URL 的工厂函数。直接来自 Python 文档:如果您迫切需要一个名为
URL
的类来封装您的 URL,请使用别名 (URL = urlparse.ParseResult
) 或创建一个适配器。urlparse
does encapsulate URLs into a class, calledParseResult
, so it can be considered a factory function for these. Straight from the Python docs:If you desperately want a class called
URL
to encapsulate your URLs, use an alias (URL = urlparse.ParseResult
) or create an adapter.迟到了大约 10 年,但今天,pydantic 提供了几种 URL 类型,可能有助于验证、存储和传递 URL;带有 类型提示 和
mypy
如今变得越来越流行,有些人可能认为这是某种标准。~10 years late to the party here, but today, pydantic provides several URL types that might be helpful for validating, storing and passing around URLs; with type hints and
mypy
becoming more and more prevalent nowadays, some might consider this some kind of standard.您可能需要考虑查看 furl 因为它可能可以满足您的需求。
You might want consider having a look at furl because it might be an answer to your needs.
截至 2018 年,我们拥有的内容:
目前只有 Furl 得到维护,但它的主要缺点是它是可变的,当然这不鼓励最佳实践。 (有一个很好的现代参考 —
pathlib
,它由不可变的类组成。)总体而言,拥有一种轻松的 OO 方法来解析和构造 URL 是很棒的。
更新
yarl 值得一看。
What we have as of 2018:
Only furl is being maintained today but its major disadvantage is that it's mutable, that doesn't encourage best practices, of course. (There is good modern reference —
pathlib
which consists of immutable classes.)Overall, having a painless OO way to parse and construct URLs is graeat.
Update
yarl is worth looking at.
urlpath 是我的 URL 对象。它镜像
pathlib Path
对象。urlpath is my go-to for a URL object. It mirrors the
pathlib Path
object.