SaltyCrane Blog — Notes on JavaScript and web development

Python urlparse example

Here is an example of how to parse a URL using Python's urlparse module. See the urlparse module documentation for more information.

from urlparse import urlparse

url = 'http://www.gurlge.com:80/path/file.html;params?a=1#fragment'
o = urlparse(url)
print o.scheme
print o.netloc
print o.hostname
print o.port
print o.path
print o.params
print o.query
print o.fragment
print o.username
print o.password

Results:

http
www.gurlge.com:80
www.gurlge.com
80
/path/file.html
params
a=1
fragment
None
None

Comments


#1 ane commented on :

Nice little example! Thanks!


#2 Devin Columbus commented on :

I dont understand how this works with an actual URL ... here it's just hardcoded. How do you extract the actual browser URL?

disqus:3110130209