Python 中是否有预定义的 URL 类?

发布于 2024-11-10 12:26:38 字数 1096 浏览 3 评论 0原文

我正在寻找类似 java.net.URL 在 python-modules、Django、Zope 或 Python 中的任何位置。 我希望它最好是从语义上原因,因为相关程序的分析结果表明URL在其中起着重要作用。结果是这样的 URL 类在该程序中也将具有很大的实际用途。

当然,我可以自己编写这样的类,但在开始重新发明轮子之前,我想先看看周围。

我确实查看了 urllib2urlparse。 urlparse 基本上具有我需要的功能,但它没有将其封装到像 java.net.URL 这样的类中。关于我对程序的分析,它的工作原理是颠倒的。

我还查看了 SplitResultParseResult 类中 urlparse 的源代码。它们具有一些基本功能,可用于子类化。但我必须将其余的 urlparse 函数重写为子类方法。

我还发现了 mxURL - Python 的灵活 URL 数据类型。它非常接近我真正想要的。只是这对于我的目的来说似乎有点过分了。

谁能建议另一种选择?我应该继续重新发明轮子吗?

我的解决方案:

为了获取 URL 类,我基本上做了两件事:

  1. urlparse.ResultMixin 继承。
  2. 定义仅调用 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:

  1. Inherit from urlparse.ResultMixin.
  2. Define function which only calls urlparse.urlparse() and transforms results to
    parameters of URL instance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

左秋 2024-11-17 12:26:38

urlparse 确实将 URL 封装到一个名为 ParseResult 的类中,因此它可以被视为这些 URL 的工厂函数。直接来自 Python 文档:

>>> urlparse('http://www.cwi.nl:80/%7Eguido/Python.html')
ParseResult(scheme='http', netloc='www.cwi.nl:80', path='/%7Eguido/Python.html',
            params='', query='', fragment='')

如果您迫切需要一个名为 URL 的类来封装您的 URL,请使用别名 (URL = urlparse.ParseResult) 或创建一个适配器。

urlparse does encapsulate URLs into a class, called ParseResult, so it can be considered a factory function for these. Straight from the Python docs:

>>> urlparse('http://www.cwi.nl:80/%7Eguido/Python.html')
ParseResult(scheme='http', netloc='www.cwi.nl:80', path='/%7Eguido/Python.html',
            params='', query='', fragment='')

If you desperately want a class called URL to encapsulate your URLs, use an alias (URL = urlparse.ParseResult) or create an adapter.

破晓 2024-11-17 12:26:38

迟到了大约 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.

乄_柒ぐ汐 2024-11-17 12:26:38

您可能需要考虑查看 furl 因为它可能可以满足您的需求。

You might want consider having a look at furl because it might be an answer to your needs.

晨与橙与城 2024-11-17 12:26:38

截至 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.

假装不在乎 2024-11-17 12:26:38

urlpath 是我的 URL 对象。它镜像 pathlib Path 对象。

urlpath is my go-to for a URL object. It mirrors the pathlib Path object.

小梨窩很甜 2024-11-17 12:26:38
>>> import urllib3
>>> help(urllib3.util.url)
...
CLASSES
Url(builtins.tuple)
    Url

class Url(Url)
 |  Url(scheme=None, auth=None, host=None, port=None, path=None, query=None, fragment=None)
 |  
 |  Data structure for representing an HTTP URL. Used as a return value for
 |  :func:`parse_url`. Both the scheme and host are normalized as they are
 |  both case-insensitive according to RFC 3986.
 |  
 |  Method resolution order:
 |      Url
 |      Url
 |      builtins.tuple
 |      builtins.object
>>> import urllib3
>>> help(urllib3.util.url)
...
CLASSES
Url(builtins.tuple)
    Url

class Url(Url)
 |  Url(scheme=None, auth=None, host=None, port=None, path=None, query=None, fragment=None)
 |  
 |  Data structure for representing an HTTP URL. Used as a return value for
 |  :func:`parse_url`. Both the scheme and host are normalized as they are
 |  both case-insensitive according to RFC 3986.
 |  
 |  Method resolution order:
 |      Url
 |      Url
 |      builtins.tuple
 |      builtins.object
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文