10

I am running an HTTP server which serves a bitmap according to the dimensions in the browser URL i.e localhost://image_x120_y30.bmp. My server is running in infinite loop and I want to get the URL any time user requests for BITMAP, and at the end I can extract the image dimensions from the URL.

The question asked here:

How to get current URL in python web page?

does not address my problem as I am running in infinite loop and I want to keep on getting the current URL so I can deliver the requested BITMAP to the user.

Pika Supports Ukraine
  • 3,394
  • 9
  • 24
  • 41
Chaudhry Waqas
  • 2,073
  • 7
  • 39
  • 60
  • possible duplicate of [How to get current URL in python web page?](http://stackoverflow.com/questions/14468862/how-to-get-current-url-in-python-web-page) – Liam May 27 '15 at 10:07

5 Answers5

9

If to use Selenium for web navigation:

from selenium import webdriver
driver = webdriver.Firefox()
print (driver.current_url)
Andersson
  • 49,746
  • 15
  • 64
  • 117
  • I am running python 2.7 and I got following error ImportError: No module named selenium @Andersson – Chaudhry Waqas May 27 '15 at 10:32
  • Selenium compatible with 2.7 as well as with 3.4. You need to install this package first and then import it within code. Try pip install selenium – Andersson May 27 '15 at 11:17
  • 1
    it is now working but it opens a new browser window of firefox , what I want is to get the url from browser. – Chaudhry Waqas May 27 '15 at 11:28
  • 1
    You can access page of required site with `driver.get('put_your_site_name')` and then get page url with `driver.current_url` after each loop iteration P.S. Please put more info about how your script works/should work or just show the part of existed code – Andersson May 27 '15 at 11:49
  • currently I am getting the requested content by parsing the HTTP request but I want if it is possible to get the url directly form browser so I can parse it to get the requested BITMAP dimensions. – Chaudhry Waqas May 27 '15 at 12:24
  • I'm using chrome and I a getting `data:,` with `self.driver.current_url` – bhattraideb May 12 '21 at 10:02
3

You can get the current url by doing path_info = request.META.get('PATH_INFO') http_host = request.META.get('HTTP_HOST'). You can add these two to get complete url. Basically request.META returns you a dictionary which contain a lot of information. You can try it.

Tasneem Haider
  • 191
  • 1
  • 8
1

I just solved a class problem similar to this. We've been using Splinter to walk through pages (you will need to download splinter and Selenium). As I walk through pages, I periodically need to pull the url of the page I'm currently on. I do that using the command new_url = browser.url Below is an example of my code.

I do this using the following code.

##import dependencies
from splinter import browser
import requests


## go to original page 
browser.visit(url)

## Loop through the page associated with each headline
for headline in titles:
    print(headline.text)
    browser.click_link_by_partial_text(headline.text)
## Now that I'm on the new page, I need to grab the url
    new_url = browser.url
    print(new_url)
## Go back to original page
    browser.visit(url)
Heather Claxton
  • 801
  • 6
  • 10
0

Below is the solution I use in Django.

For eg.,. if browser url is https://www.example.com/dashboard

try:
    from urlparse import urlparse
except ImportError:
    from urllib.parse import urlparse

frontend_url = request.META.get('HTTP_REFERER')
url = urlparse(frontend_url)
print (url)
# ParseResult(scheme='https', netloc='example.com', path='/dashboard', params='', query='', fragment='')
SuperNova
  • 21,204
  • 6
  • 80
  • 55
-7

You could use the requests module:

import requests


link = "https://stackoverflow.com"
data = requests.request("GET", link)
url = data.url
Sipher_
  • 71
  • 8
  • 2
    This solution doesn't answer what he needs. It also has errors as you passed _url_ instead of variable _link_ as parameter to request. – Paulo Fabrício Apr 13 '18 at 18:06