Wednesday, November 7, 2012

python url parse

Python got awesome module called urlparse. When you want different values from url, you might think of doing substring. Which is very risky and bad practice.

Here is the simple and easy way to do it in python -

>>> str = ""
>>> from urlparse import urlparse
>>> o = urlparse(str)
>>> o
ParseResult(scheme='http', netloc='', path='/api/v1/items/26/', params='', query='', fragment='')

>>> o.path

That was easy!

You can do much more using this module, please checkout the more details on this Reference link.

No comments:

Post a Comment