0

I have a mini project about "Weather App", i use Nominatim to get location but it seems like not working.

Here is the code

from tkinter import *
import tkinter as tk
from geopy.geocoders import Nominatim
from tkinter import ttk,messagebox
from timezonefinder import TimezoneFinder
from datetime import *
import requests
import pytz
from PIL import Image, ImageTk

root = Tk()
root.title("Weather App")
root.geometry("900x500+300+200")
root.configure(bg = "#57adff")
root.resizable(False, False)



#get weather

def getWeather():
    city = textfield.get()
    geolocator = Nominatim(user_agent = "geoapiExercises")
    location= geolocator.geocode(city, timeout = None)
    obj = TimezoneFinder()

    result = obj.timezone_at(lng = location.longitude, lat = location.latitude)

    timezone.config(text=result)
    long_lat.config(text = f"{round(location.latitude,4)}°N, {round(location.longitude,4)}°E")

    home = pytz.timezone(result)
    local_time = datetime.now(home)
    current_time = local_time.strftime("%I:%M %p")
    clock.config(text = current_time)

and the problem

        Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Users\84985\AppData\Local\Programs\Python\Python311\Lib\site-packages\geopy\geocoders\base.py", line 368, in _call_geocoder
    result = self.adapter.get_json(url, timeout=timeout, headers=req_headers)
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\84985\AppData\Local\Programs\Python\Python311\Lib\site-packages\geopy\adapters.py", line 472, in get_json
    resp = self._request(url, timeout=timeout, headers=headers)
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\84985\AppData\Local\Programs\Python\Python311\Lib\site-packages\geopy\adapters.py", line 500, in _request
    raise AdapterHTTPError(
geopy.adapters.AdapterHTTPError: Non-successful status code 403

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
File "C:\Users\84985\AppData\Local\Programs\Python\Python311\Lib\tkinter\__init__.py", line 1948, in __call__
    return self.func(*args)
        ^^^^^^^^^^^^^^^^
File "v:\PYTHON\Python Weather Project\weather.py", line 24, in getWeather
    location= geolocator.geocode(city, timeout = None)
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\84985\AppData\Local\Programs\Python\Python311\Lib\site-packages\geopy\geocoders\nominatim.py", line 297, in geocode
    return self._call_geocoder(url, callback, timeout=timeout)
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\84985\AppData\Local\Programs\Python\Python311\Lib\site-packages\geopy\geocoders\base.py", line 388, in _call_geocoder
    res = self._adapter_error_handler(error)
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\84985\AppData\Local\Programs\Python\Python311\Lib\site-packages\geopy\geocoders\base.py", line 411, in _adapter_error_handler
    raise exc_cls(str(error)) from error
geopy.exc.GeocoderInsufficientPrivileges: Non-successful status code 403
5
  • Please paste the error message and the code here rather adding a screenshot.
    – Padmanabha
    Commented Oct 10, 2023 at 15:27
  • okay, i've just fixed it
    – Ban Mike
    Commented Oct 10, 2023 at 15:59
  • 1
    Looks like you have not correctly authenticated so the server returned a 403 response
    – Ian Turton
    Commented Oct 10, 2023 at 22:26
  • 1
    you probably need to create a user_agent; see gis.stackexchange.com/questions/293615/…
    – Mike T
    Commented Oct 10, 2023 at 23:22
  • thanks for your help!!!
    – Ban Mike
    Commented Oct 13, 2023 at 0:35

1 Answer 1

1

You use: geolocator = Nominatim(user_agent = "geoapiExercises")

Replace with this: geolocator = Nominatim(user_agent = "abcd")

1
  • As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.
    – Community Bot
    Commented Oct 27, 2023 at 2:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.