【问题标题】:How to get coordinates from a Wikipedia page using Python?如何使用 Python 从维基百科页面获取坐标?
【发布时间】:2020-12-05 21:12:37
【问题描述】:

我想获取给定维基百科页面的坐标。我尝试使用 Wikipedia API,但是唯一相关的方法是 geosearch(),它返回给定一对坐标的页面,而我想要完全相反。

【问题讨论】:

    标签: python web-scraping geolocation wikipedia-api


    【解决方案1】:

    您没有提到您使用的是哪个 Python 库,但一般来说,例如要使用Geosearch API 获取Washington, D.C. 文章的坐标,可以使用以下网址:

    https://en.wikipedia.org/w/api.php?action=query&prop=coordinates&titles=Washington,%20D.C.&%20format=json

    【讨论】:

      【解决方案2】:

      不确定是否使用 Wikipedia API 来完成,但可以很容易地单独完成。

      import requests
      from bs4 import BeautifulSoup as bs
      req = requests.get("https://en.wikipedia.org/wiki/Calgary").text
      soup = bs(req, 'lxml')
      latitude = soup.find("span", {"class": "latitude"})
      longitude = soup.find("span", {"class": "longitude"})
      print(latitude.text, longitude.text)
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-09-15
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多