1

I know if I want to send an HTTP request, I can form it as a dictionary before sending it:

payload = dict(
    username="something",
    password="something_else"
)

r = requests.get('http://example.com', params=payload)

Does it do the opposite as well? If my request happens to redirect me to another URL, I can see where I was redirected with:

print r.url

But if that URL happens to be something like:

http://example.com/somepage.htm?varone=test&vartwo=3451&varthree=something

and I happen to want to pull out the vartwo from that URL (the string 3451), does the requests module provide any easy way to do this just by specifying that I want the value of "vartwo"? Another dictionary, or mapping object of some sort perhaps? Or is my only option to use urllib.unquote to decode the url, and parse it as a string?

John
  • 2,551
  • 3
  • 30
  • 55

1 Answers1

2

Use urlparse module (Python 2.x; in Python 3.x, it was renamed to urllib.parse).

from urlparse import urlparse

url = 'http://www.gurlge.com:80/path/file.html;params?a=1#fragment'
o = urlparse(url)

print o.params
Dmitriy Khudorozhkov
  • 1,624
  • 12
  • 24