使用python从浏览器获取当前URL
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30479290/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Get current URL from browser using python
提问by Chaudhry Waqas
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.
我正在运行一个 HTTP 服务器,它根据浏览器 URL 中的尺寸提供位图,即localhost://image_x120_y30.bmp
. 我的服务器在无限循环中运行,我想在用户请求 BITMAP 时随时获取 URL,最后我可以从 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.
没有解决我的问题,因为我在无限循环中运行,我想继续获取当前 URL,以便我可以将请求的 BITMAP 交付给用户。
采纳答案by Andersson
If to use Selenium for web navigation:
如果使用 Selenium 进行网页导航:
from selenium import webdriver
driver = webdriver.Firefox()
print (driver.current_url)
回答by Tasneem Haider
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.
您可以通过执行
path_info = request.META.get('PATH_INFO')
http_host = request.META.get('HTTP_HOST')
. 您可以添加这两个以获得完整的网址。基本上 request.META 会返回一个包含大量信息的字典。你可以试试看。
回答by Sipher_
You could use the requests
module:
您可以使用该requests
模块:
import requests
link = "https://stackoverflow.com"
data = requests.request("GET", link)
url = data.url
回答by Heather Claxton
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.
我刚刚解决了一个类似的类问题。我们一直在使用 Splinter 浏览页面(您需要下载 Splinter 和 Selenium)。当我浏览页面时,我需要定期拉出我当前所在页面的 url。我使用命令 new_url = browser.url 做到这一点 下面是我的代码示例。
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)
回答by SuperNova
Below is the solution I use in Django.
以下是我在 Django 中使用的解决方案。
For eg.,. if browser url is https://www.example.com/dashboard
例如,。如果浏览器网址是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='')