TIMEX
TIMEX

Reputation: 272134

Does Python 2.6 have a built in URL parameters parser?

Given a URl, how can I get a dictionary of parameters back?

Upvotes: 3

Views: 1903

Answers (2)

Senthil Kumaran
Senthil Kumaran

Reputation: 56941

ofcourse, it is called urlparse. However, instead of a dictionary, the urlsplit and urlparse methods output a namedtuple object, which you are easily reference by member attributes. If dictionary is a must, you can construct one using those named tuple values too. And if you want to parse the query params, use parse_qs and parse_qsl from the same module

Upvotes: 2

Guillaume
Guillaume

Reputation: 1032

http://docs.python.org/library/urlparse.html

edit: It returns a tuple of the url parameters. If you absolutely need to have it be in a dictionnary looks like you will have to implement it yourself. (get the tuple then assign it to dict)

string = "addressing scheme network location path query fragment_identifier".split()
url = urlparse.urlsplit("url")

dictionary = dict(zip(string,url))

Not sure if that dictionary line is valid, but it's something like that.

Upvotes: 7

Related Questions