代码之家  ›  专栏  ›  技术社区  ›  u0b34a0f6ae

使用urlparse(Python)解析自定义URI

  •  13
  • u0b34a0f6ae  · 技术社区  · 17 年前

    >>> urlparse.urlparse("qqqq://base/id#hint")
    ('qqqq', '', '//base/id#hint', '', '', '')
    >>> urlparse.urlparse("http://base/id#hint")
    ('http', 'base', '/id', '', '', 'hint')
    

    import urlparse
    
    SCHEME = "qqqq"
    
    # One would hope that there was a better way to do this
    urlparse.uses_netloc.append(SCHEME)
    urlparse.uses_fragment.append(SCHEME)
    

    6 回复  |  直到 10 年前
        1
  •  22
  •   toothygoose    15 年前

    import urlparse
    
    def register_scheme(scheme):
        for method in filter(lambda s: s.startswith('uses_'), dir(urlparse)):
            getattr(urlparse, method).append(scheme)
    
    register_scheme('moose')
    

    uses_fragment
    uses_netloc
    uses_params
    uses_query
    uses_relative
    

    urlparse.urlparse('moose://username:password@hostname:port/path?query=value#fragment')._asdict()
    => {'fragment': 'fragment', 'netloc': 'username:password@hostname:port', 'params': '', 'query': 'query=value', 'path': '/path', 'scheme': 'moose'}
    
        2
  •  3
  •   Ned Batchelder    17 年前

    parts = urlparse.urlparse("qqqq://base/id#hint")
    fake_url = "http:" + parts[2]
    parts2 = urlparse.urlparse(fake_url)
    
        3
  •  3
  •   sumid    13 年前

    还有一个图书馆叫 furl

    >>>import furl
    >>>f=furl.furl("qqqq://base/id#hint");
    >>>f.scheme
    'qqqq' 
    
    >>> f.host
    'base'  
    >>> f.path
    Path('/id')
    >>>  f.path.segments
    ['id']
    >>> f.fragment                                                                                                                                                                                                                                                                 
    Fragment('hint')   
    >>> f.fragmentstr                                                                                                                                                                                                                                                              
    'hint'
    
        4
  •  2
  •   OrangeDog    10 年前

    Python 2.7.10 (default, May 23 2015, 09:40:32) [MSC v.1500 32 bit (Intel)] on win32
    >>> import urlparse
    >>> urlparse.urlparse("qqqq://base/id#hint")
    ParseResult(scheme='qqqq', netloc='base', path='/id', params='', query='', fragment='hint')
    
        5
  •  1
  •   Joe Crobak    15 年前

    >>> SCHEME="qqqq"
    >>> url="qqqq://base/id#hint"[len(SCHEME)+1:]
    >>> url
    '//base/id#hint'
    >>> urlparse.urlparse(url)
    ('', 'base', '/id', '', '', 'hint')
    

    $ python2.6 -c 'import urlparse; print urlparse.urlparse("qqqq://base/id#hint")'
    ParseResult(scheme='qqqq', netloc='base', path='/id#hint', params='', query='', fragment='')
    
        6
  •  0
  •   homm    12 年前

    yurl

    >>> import yurl
    >>> yurl.URL('qqqq://base/id#hint')
    URLBase(scheme='qqqq', userinfo=u'', host='base', port='', path='/id', query='', fragment='hint')
    
    推荐文章