Get current browser url using python

I am using an HTTP server that serves a bitmap according to the size of the browser URL, i.e. localhost://image_x120_y30.bmp . My server runs in an infinite loop, and I want to get the URL every time the user requests BITMAP, and in the end I can extract the image sizes from the URL.

The question asked here:

How to get current url on python webpage?

does not solve my problem, since I work in an infinite loop, and I want to keep getting the current URL so that I can deliver the requested BITMAP to the user.

+7
source share
4 answers

If you use Selenium for web navigation:

 from selenium import webdriver driver = webdriver.Firefox() print (driver.current_url) 
+5
source

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 the full URL. Basically, request.META returns you a dictionary that contains a lot of information. You can try.

+2
source

You can use the requests module:

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

I just solved a class problem like this. We used Splinter to view the pages (you need to download Splinter and Selenium). When I go through the pages, I periodically need to pull the URL of the page I'm currently on. I do this with the command new_url = browser.url The following is an example of my code.

I am doing 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) 
0
source

Source: https://habr.com/ru/post/987997/


All Articles