有没有更简洁的方法来修改 Python 2 中 URL 的某些部分?
例如
http://foo/bar -> http://foo/yah
目前,我正在这样做:
import urlparse
url = 'http://foo/bar'
# Modify path component of URL from 'bar' to 'yah'
# Use nasty convert-to-list hack due to urlparse.ParseResult being immutable
parts = list(urlparse.urlparse(url))
parts[2] = 'yah'
url = urlparse.urlunparse(parts)
有更清洁的解决方案吗?