首页 > 解决方案 > 通过抓取网站获取 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 位置)

标签: pythonpython-3.xbeautifulsoupgpspython-requests-html

解决方案


您可以改用地理编码器

简单地做,

import geocoder
g = geocoder.ip('me')
print(g.latlng)

推荐阅读