5

In Python's urlparse, you can use urlparse to parse the URL, and then parse_qsl to parse the query.

I want to remove a query (name, value) pair, and then reconstruct the URL.

There is a urlunparse method, but no unparse_qsl method.

What is the correct way to reconstruct the query from the qsl list?

Joseph Turian
  • 15,430
  • 14
  • 47
  • 62

2 Answers2

2

The function urllib.urlencode is appropriate.

Joseph Turian
  • 15,430
  • 14
  • 47
  • 62
2
>>> urlparse.parse_qsl('q=stackoverflow')
[('q', 'stackoverflow')]

>>> urllib.urlencode(urlparse.parse_qsl('q=stackoverflow'))
'q=stackoverflow'
Senthil Kumaran
  • 54,681
  • 14
  • 94
  • 131