【问题标题】:How to get the exact location(Latitude and Longitude) of us using python?如何使用python获取我们的确切位置(纬度和经度)?
【发布时间】:2018-03-03 17:25:17
【问题描述】:

我想找到我现在居住的位置的坐标。我尝试使用这样的代码:

send_url = 'http://freegeoip.net/json'
r = requests.get(send_url)
j = json.loads(r.text)
lat = j['latitude']
lon = j['longitude']

它给了我城市坐标。但我现在想要我所在位置的坐标。如何获得?

【问题讨论】:

  • 我会用这个:whataremycoordinates.com
  • 上面代码中的url要不要替换?
  • 你必须得到页面的 html 然后解析它。我将发布一个示例。
  • 是的,这会很有用
  • 您认为计算机或网站如何知道您的确切位置?

标签: python python-requests latitude-longitude


【解决方案1】:

这对我有用

from selenium import webbrowser
a=webdriver.Chrome()
a.get("https://www.google.com/maps")
print(a.execute_script("window.location"))

【讨论】:

  • 它对我不起作用。结果显示无。我正在使用 selenium 的 webdriver
  • 嗯。为我工作
  • 你能用 window.location 取悦我吗
【解决方案2】:
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.common.by import By
import json
import time

s = Service(ChromeDriverManager().install())
driver = webdriver.Chrome(service=s)
driver.get('https://www.google.com')

string = '''
   function getLocation(callback) {
        if (navigator.geolocation) {
            navigator.geolocation.getCurrentPosition(function(position) {
                var myjson = {"latitude":position.coords.latitude, "longitude":position.coords.longitude};
                console.log(position);
                console.log(position.coords.latitude);
                console.log(position.coords.longitude); 
                var stringJson = JSON.stringify(myjson);
                callback(stringJson);
            });
        } else {
            console.log("Geolocation is not supported by this browser.");
        }
    }
    
    getLocation(function(callback) {
        console.log(callback);
        const para = document.createElement("p");
        para.innerHTML = callback;
        para.id = "location";
        document.body.appendChild(para);
    });'''.replace('\n', '').replace('\t', '')


# repeat this code
driver.execute_script(string)
time.sleep(2)
res = driver.find_element(By.ID, "location")
loc = res.text
locDict = json.loads(loc)
print("latitude: %f, longitude: %f" % (locDict["latitude"], locDict["longitude"]))

我使用这个 python 代码来获取位置。我认为你必须安装 chrome 才能运行它:v

由于我不是很懂,这只是我的猜测,所以如果我错了,请纠正我: 首先,我使用 webdriver。我打算打开 Chrome 浏览器并使用网络浏览器中的 javascript 函数来获取我的位置。

运行此代码时,Chrome 浏览器将打开。不要关闭它,否则代码将不起作用

那么,变量字符串就是一堆javascript代码。此代码将在网络浏览器上执行,地址为www.google.com,使用driver.execute_script(string)

该javascript代码的目的是使用navigator.geolocation.getCurrentPosition()获取位置g,然后将其写入JSON字符串中。然后,我创建了一个 id="location" 的 <p> 元素,但其中包含 JSON 字符串,然后附加到 Web。在代码运行完成后,您会在网站底部看到一个 json 字符串

之后,我需要找到

在我之前插入的网络中具有 id="location" 的元素driver.find_element(By.ID, "location"),获取该 JSON 字符串,对其进行解析并获取纬度和经度的值

你可能会问我为什么要创建一个<p> 元素并将其放到网络上。嗯,执行json字符串的时候,找不到从position.coords.latitude和position.coords.longitude返回经纬度值的方法,所以决定写在网上,以后再找。

【讨论】:

  • 正如目前所写,您的答案尚不清楚。请edit 添加其他详细信息,以帮助其他人了解这如何解决所提出的问题。你可以找到更多关于如何写好答案的信息in the help center
猜你喜欢
  • 2018-11-08
  • 2012-11-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多