简体   繁体   English

Python 中是否有预定义的 class 用于 URL?

[英]Is there predefined class for URL in Python?

I am looking for something like java.net.URL in python-modules, Django, Zope or wherever in Python.我正在 python 模块、Django、Zope 或 Python 中的任何地方寻找类似java.net.URL的东西。 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.我最好从语义的原因上看,因为相关程序的分析结果表明URL在其中起着至关重要的作用。 The consequence is that such URL class also will have great practical usage in that program.结果是这样的 URL class 在该程序中也将具有很大的实际用途。

Of course I could write such class on my own, but I'd like to look around before I start to reinvent the wheel .当然,我可以自己写这样的 class,但我想在开始重新发明轮子之前先看看周围。

I did look at urllib2 and urlparse .我确实看过urllib2urlparse The urlparse basically has the functionality I need, but it doesn't encapsulate it into a class like java.net.URL . urlparse基本上具有我需要的功能,但它没有将其封装到 class 之类的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 .我还在SplitResultParseResult类中查看了urlparse的源代码。 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.但是我必须将 urlparse 函数的 rest 重写为子类方法。

I found also mxURL - Flexible URL Datatype for Python .我还发现了 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:为了得到我的 URL class 我基本上做了两件事:

  1. Inherit from urlparse.ResultMixin .继承自urlparse.ResultMixin
  2. Define function which only calls urlparse.urlparse() and transforms results to parameters of URL instance.定义 function ,它只调用urlparse.urlparse()并将结果转换为 URL 实例的参数。

urlparse does encapsulate URLs into a class, called ParseResult , so it can be considered a factory function for these. urlparse确实将 URL 封装到 class 中,称为ParseResult ,因此可以将其视为工厂 function 。 Straight from the Python docs:直接来自 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='')

If you desperately want a class called URL to encapsulate your URLs, use an alias ( URL = urlparse.ParseResult ) or create an adapter.如果您非常想要一个名为 URL 的URL来封装您的 URL,请使用别名( URL = urlparse.ParseResult )或创建一个适配器。

You might want consider having a look at furl because it might be an answer to your needs.您可能需要考虑查看furl ,因为它可能是您需要的答案。

What we have as of 2018: 2018年我们拥有的:

Only furl is being maintained today but its major disadvantage is that it's mutable, that doesn't encourage best practices, of course.今天只维护了 furl,但它的主要缺点是它是可变的,这当然不鼓励最佳实践。 (There is good modern reference — pathlib which consists of immutable classes.) (有一个很好的现代参考pathlib ,它由不可变的类组成。)

Overall, having a painless OO way to parse and construct URLs is graeat.总的来说,拥有一种轻松的 OO 方法来解析和构造 URL 是非常棒的。

Update更新

yarl is worth looking at.亚尔值得一看。

~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;晚了约 10 年,但今天, pydantic提供了几种 URL 类型,它们可能有助于验证、存储和传递 URL; with type hints and mypy becoming more and more prevalent nowadays, some might consider this some kind of standard.随着类型提示mypy现在越来越流行,有些人可能会认为这是某种标准。

urlpath is my go-to for a URL object. urlpath是 URL object 的首选。 It mirrors the pathlib Path object.它反映了pathlib Path object。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM