Changing URL components in Python 2

Is there a cleaner way to change some parts of a url in Python 2?

for instance

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

I am currently doing this:

 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) 

Is there a cleaner solution?

+6
source share
2 answers

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

Do not turn this namedtuple into a list, but use the utility method provided only for this task:

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

The namedtuple._replace() method allows you to create a new copy with specific elements replaced. ParseResult.geturl() method then reattaches the parts to the 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 an error report (with correction) to resolve the problem with the documentation problem.

+16
source

I think the right way to do this is exactly so.

As using _replace private methods or variables are not suggested.

 from urlparse import urlparse, urlunparse res = urlparse('http://www.goog.com:80/this/is/path/;param=paramval?q=val&foo=bar#hash') l_res = list(res) # this willhave ['http', 'www.goog.com:80', '/this/is/path/', 'param=paramval', 'q=val&foo=bar', 'hash'] l_res[2] = '/new/path' urlunparse(l_res) # outputs 'http://www.goog.com:80/new/path;param=paramval?q=val&foo=bar#hash' 
-1
source

Source: https://habr.com/ru/post/970797/


All Articles