Modify URL components in Python 2

后端 未结 2 1255
星月不相逢
星月不相逢 2021-02-14 10:10

Is there a cleaner way to modify some parts of a URL in Python 2?

For example

http://foo/bar -> http://foo/yah

At present, I\'m doin

2条回答
  •  太阳男子
    2021-02-14 11:03

    Unfortunately, the documentation is out of date; the results produced by urlparse.urlparse() (and urlparse.urlsplit()) use a collections.namedtuple()-produced class as a base.

    Don't turn this namedtuple into a list, but make use of the utility method provided for just this task:

    parts = urlparse.urlparse(url)
    parts = parts._replace(path='yah')
    
    url = parts.geturl()
    

    The namedtuple._replace() method lets you create a new copy with specific elements replaced. The ParseResult.geturl() method then re-joins the parts into a url for you.

    Demo:

    >>> import urlparse
    >>> url = 'http://foo/bar'
    >>> parts = urlparse.urlparse(url)
    >>> parts = parts._replace(path='yah')
    >>> parts.geturl()
    'http://foo/yah'
    

    mgilson filed a bug report (with patch) to address the documentation issue.

提交回复
热议问题