Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the (current url + query string) using python code with urllib2?

I need to read the current url with the query string which i used ?

Means i need to get the browser current address bar url..

like image 416
Ayyappan Anbalagan Avatar asked Sep 10 '25 18:09

Ayyappan Anbalagan


1 Answers

A urllib2.Request object provides a geturl() method, which returns the full URL of a request. You may then pass it to urlparse.urlparse(), which splits a URL into six components of every URL. Then you may access the query part via query attribute.

An example:

>>> from urllib2 import urlopen
>>> from urlparse import urlparse
>>> req = urlopen('http://capitalfm.com/?foo=bar')
>>> req.geturl()
'http://www.capitalfm.com/?foo=bar'
>>> url = urlparse(req.geturl())
>>> url.query
'foo=bar'
like image 109
Michal Chruszcz Avatar answered Sep 13 '25 08:09

Michal Chruszcz