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.

1

6 Answers 6

14

If to use Selenium for web navigation:

from selenium import webdriver
driver = webdriver.Firefox()
print (driver.current_url)
Sign up to request clarification or add additional context in comments.

6 Comments

I am running python 2.7 and I got following error ImportError: No module named selenium @Andersson
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
it is now working but it opens a new browser window of firefox , what I want is to get the url from browser.
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
I'm using chrome and I a getting data:, with self.driver.current_url
|
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.

Comments

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)

Comments

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='')

Comments

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}

1 Comment

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.
-8

You could use the requests module:

import requests


link = "https://stackoverflow.com"
data = requests.request("GET", link)
url = data.url

1 Comment

This solution doesn't answer what he needs. It also has errors as you passed url instead of variable link as parameter to request.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.