【问题标题】:Get gps location by scraping a website | Python通过抓取网站获取 gps 位置 | Python
【发布时间】:2020-08-11 14:26:44
【问题描述】:

在我的 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 位置)

【问题讨论】:

  • 您需要发出与按钮相同的请求
  • 我该怎么做呢?
  • 你必须通过浏览器的开发者工具弄清楚请求是什么,然后用 python 复制

标签: python python-3.x beautifulsoup gps python-requests-html


【解决方案1】:

您可以改用地理编码器

简单地做,

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

【讨论】:

  • 对不起。地理编码器使用 IP 来获取位置。我为此做了几个例子。但它返回的位置却在数百公里之外。我必须使用 GPS。
  • 好的,抱歉,这将是一个更好的解决方案codeburst.io/…
  • 我会努力的,谢谢!但也许这里会有解决方案所以我不会删除问题
【解决方案2】:

正如 MukundKulkarni 提到的,有本教程使用 Selenium 来解析网站: How I Understood: Getting accurate current geolocation using Python Web-scraping and Selenium

显然,他们不必先允许位置跟踪。我将使用 Selenium 进行进一步的网络抓取。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-17
    • 2013-11-16
    相关资源
    最近更新 更多