13

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,612
  • 10
  • 26
  • 42
StealthTrails
  • 2,281
  • 8
  • 43
  • 67
  • 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

6 Answers6

11

If to use Selenium for web navigation:

from selenium import webdriver
driver = webdriver.Firefox()
print (driver.current_url)
Andersson
  • 51,635
  • 17
  • 77
  • 129
  • I am running python 2.7 and I got following error ImportError: No module named selenium @Andersson – StealthTrails 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. – StealthTrails 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. – StealthTrails May 27 '15 at 12:24
  • 1
    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
  • 359
  • 3
  • 9
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
  • 1,001
  • 8
  • 11
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
  • 25,512
  • 7
  • 93
  • 64
0
    Hello you can use below code in order to achieve URL from open browser
    
    import os
    import webbrowser
    import pyperclip
    import time
    import keyboard
    import pygetwindow as gw
    import pyautogui
    
    @app.route("/")
    def redirect_to_authorization():
        redirect_url = f"https://www.google.com"
        webbrowser.open(redirect_url)
        time.sleep(5)
        browser_window = gw.getActiveWindow()
        browser_window.activate()
        pyautogui.hotkey('ctrl', 'l')
        time.sleep(2)
        pyautogui.hotkey('ctrl', 'c')
        keyboard.press_and_release('ctrl + c')
        time.sleep(0.5) 
        url = pyperclip.paste()
        print(url)
        # os.system("taskkill /f /im chrome.exe")
        index = url.find('code=')
        if index != -1:
            code = url[index + len('code='):]
            print("Code:", code)
        # os.system("taskkill /f /im chrome.exe")
        return {"Token" : code}

# Or you can use below code too

    @app.route("/CodeTwo")
    def redirect_to_authorization():
        redirect_url = f"https://www.google.com"
        webbrowser.open(redirect_url)
        time.sleep(5)
        active_window = gw.getActiveWindow()
        if active_window is not None:
            title = active_window.title
            if " - Google Chrome" in title:
                # Extract the URL from the title
                url = title.split(" - Google Chrome")[0]
                return {"Token" : url}
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jun 14 '23 at 11:54
-7

You could use the requests module:

import requests


link = "https://stackoverflow.com"
data = requests.request("GET", link)
url = data.url
Sipher_
  • 71
  • 9
  • 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