【问题标题】:Does the Python standard library contain a module for manipulating URIs?Python 标准库是否包含用于操作 URI 的模块?
【发布时间】:2010-02-08 05:49:47
【问题描述】:

我想将 URI 传递给构造函数并取回一个对象,我可以在该对象上调用 obj.type、obj.host、obj.port 等。urllib2 模块的“Request”对象接近我需要的,但还不够。

【问题讨论】:

  • 你需要什么Request对象不做的?

标签: python url string


【解决方案1】:

可能类似于urlparse module

urlparse 模块在 Python 3.0 中重命名为 urllib.parse

来自文档:

>>> from urlparse import urlparse
>>> o = urlparse('http://www.cwi.nl:80/%7Eguido/Python.html')
>>> o   # doctest: +NORMALIZE_WHITESPACE
    ParseResult(scheme='http', netloc='www.cwi.nl:80', path='/%7Eguido/Python.html',
    params='', query='', fragment='')
>>> o.scheme
'http'
>>> o.port
80
>>> o.geturl()
'http://www.cwi.nl:80/%7Eguido/Python.html'

【讨论】:

  • 谢谢,这个模块正是我需要的。
猜你喜欢
  • 2014-04-07
  • 2020-12-29
  • 1970-01-01
  • 1970-01-01
  • 2017-02-21
  • 2018-04-03
  • 2011-12-08
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多