pythonweb-scrapinggeolocationwikipedia-api

How to get coordinates from a Wikipedia page using Python?


I want to get the coordinates of a given Wikipedia page. I tried to use the Wikipedia API, however the only relevant method is the geosearch() that returns a page given a pair of coordinates and i want the exact opposite.


Solution

  • Not sure about doing it with the Wikipedia API, but it can easily be done separately.

    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)