Get current URL from browser using python
Asked Answered
K

6

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.

Khalil answered 27/5, 2015 at 10:4 Comment(1)
possible duplicate of How to get current URL in python web page?Salzman
W
12

If to use Selenium for web navigation:

from selenium import webdriver
driver = webdriver.Firefox()
print (driver.current_url)
Wilder answered 27/5, 2015 at 10:16 Comment(6)
I am running python 2.7 and I got following error ImportError: No module named selenium @WilderKhalil
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 seleniumWilder
it is now working but it opens a new browser window of firefox , what I want is to get the url from browser.Khalil
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 codeWilder
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.Khalil
I'm using chrome and I a getting data:, with self.driver.current_urlKrilov
W
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.

Wilkes answered 11/3, 2016 at 13:35 Comment(0)
S
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)
Soluble answered 10/5, 2019 at 20:0 Comment(0)
S
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='')
Straiten answered 22/1, 2020 at 6:44 Comment(0)
R
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}
Redemptioner answered 14/6, 2023 at 9:10 Comment(1)
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.Edi
C
-7

You could use the requests module:

import requests


link = "https://stackoverflow.com"
data = requests.request("GET", link)
url = data.url
Cognate answered 5/1, 2018 at 5:28 Comment(1)
This solution doesn't answer what he needs. It also has errors as you passed url instead of variable link as parameter to request.Whittling

© 2022 - 2024 — McMap. All rights reserved.