python - 通过抓取网站获取 gps 位置 | Python
问题描述
在我的 python 项目中,我想获取我的 gps 位置。所以我想解析一个网站,它给我这些信息: https://www.where-am-i.net/完美!所以我编写了以下脚本来解析纬度,经度:
from bs4 import BeautifulSoup
import requests
#website = "https://schlechtewitze.com/kurze-witze"
website = "https://www.where-am-i.net/"
source = requests.get(website).text
soup = BeautifulSoup(source, 'lxml')
for div in soup.find_all('div', class_ = "location"):
summary = div.p.text
print(summary)
print()
它几乎可以工作,但几乎可以:它返回:我的位置是:0.0, 0.0 如您所见,它返回的不是真实位置,因为我的计算机(Windows 10)阻止了位置请求。例如,当我在浏览器中打开它时:
只有在我单击允许(德语中的Zulassen)网站获取我的计算机位置后,才会显示该位置。
但是在脚本中我不能“点击这个按钮”。
问题结果: 如何让网站通过脚本(美汤、请求)获取我的位置?
(我不介意是否有其他解决方案(没有 bs4)来获取我的 GPS 位置)
解决方案
您可以改用地理编码器
简单地做,
import geocoder
g = geocoder.ip('me')
print(g.latlng)
推荐阅读
- javascript - 如何在动态html表格中实现分页
- javascript - 我想要两个 mysql 表加入 group_concat 结果在 json 数组
- java - Spark Structured Streaming - 在有状态的流处理中使用窗口操作进行事件处理
- nlp - 带有句子的未知标记
- amazon-web-services - fluentd 无法连接到 elasticsearch SSL_connect 返回=1 errno=0 state=error: 错误的版本号 (OpenSSL::SSL::SSLError)
- wix3.5 - 如何在 wix 工具集中杀死受保护的进程
- python - 打印 Tensorflow 网络中每一层的层形状摘要
- c# - 我无法在 Unity 2D 中生成新对象
- kotlin - 有什么优雅的方法可以在 Kotlin 中将 Int 转换为 BigDecimal?
- javascript - 为什么我得到 [object object ] 而不是 JSON?