使用 python 从浏览器获取当前 URL
Get current URL from browser using python
我是 运行 一个 HTTP 服务器,它根据浏览器 URL 中的尺寸提供位图,即 localhost://image_x120_y30.bmp
。我的服务器处于 运行 无限循环中,我想在用户请求 BITMAP 时获取 URL,最后我可以从 URL 中提取图像尺寸。
这里问的问题:
How to get current URL in python web page?
没有解决我的问题,因为我 运行 处于无限循环中,我想继续获取当前 URL 以便我可以将请求的 BITMAP 传递给用户。
如果使用 Selenium 进行网络导航:
from selenium import webdriver
driver = webdriver.Firefox()
print (driver.current_url)
您可以通过以下方式获取当前 url
path_info = request.META.get('PATH_INFO')
http_host = request.META.get('HTTP_HOST')
。
您可以将这两个相加以获得完整 url。
基本上request.METAreturns你一个字典里面包含了很多信息。你可以试试。
您可以使用 requests
模块:
import requests
link = "https://whosebug.com"
data = requests.request("GET", link)
url = data.url
我刚刚解决了一个与此类似的 class 问题。
我们一直在使用 Splinter 浏览页面(您需要下载 splinter 和 Selenium)。当我浏览页面时,我需要周期性地拉取当前所在页面的 url。我使用命令 new_url = browser.url 执行此操作,下面是我的代码示例。
我使用以下代码执行此操作。
##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)
下面是我在 Django 中使用的解决方案。
例如,。如果浏览器 url 是 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='')
我是 运行 一个 HTTP 服务器,它根据浏览器 URL 中的尺寸提供位图,即 localhost://image_x120_y30.bmp
。我的服务器处于 运行 无限循环中,我想在用户请求 BITMAP 时获取 URL,最后我可以从 URL 中提取图像尺寸。
这里问的问题:
How to get current URL in python web page?
没有解决我的问题,因为我 运行 处于无限循环中,我想继续获取当前 URL 以便我可以将请求的 BITMAP 传递给用户。
如果使用 Selenium 进行网络导航:
from selenium import webdriver
driver = webdriver.Firefox()
print (driver.current_url)
您可以通过以下方式获取当前 url
path_info = request.META.get('PATH_INFO')
http_host = request.META.get('HTTP_HOST')
。
您可以将这两个相加以获得完整 url。
基本上request.METAreturns你一个字典里面包含了很多信息。你可以试试。
您可以使用 requests
模块:
import requests
link = "https://whosebug.com"
data = requests.request("GET", link)
url = data.url
我刚刚解决了一个与此类似的 class 问题。 我们一直在使用 Splinter 浏览页面(您需要下载 splinter 和 Selenium)。当我浏览页面时,我需要周期性地拉取当前所在页面的 url。我使用命令 new_url = browser.url 执行此操作,下面是我的代码示例。
我使用以下代码执行此操作。
##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)
下面是我在 Django 中使用的解决方案。
例如,。如果浏览器 url 是 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='')