-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPythonBasics_API-basedWeatherDataRetrievalandSearchFunction.py
84 lines (57 loc) · 2.44 KB
/
PythonBasics_API-basedWeatherDataRetrievalandSearchFunction.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
import json
from requests import get
from pprint import pprint
import requests
from pprint import pprint
api_key = "key"
city = ("Marrakesh")
base_url = "http://api.weatherstack.com/current"
url = f"{base_url}?access_key={api_key}&query={city}"
response = requests.get(url)
data = response.json()
pprint(data)
#ekrana şehrin current temperature, current wind_speed ve location name değerlerini yazdıralım.
if response.status_code == 200 and 'current' in data:
location_name = data['location']['name']
current_temperature = data['current']['temperature']
current_wind_speed = data['current']['wind_speed']
print(f'City: {location_name}')
print(f'Current Temperature: {current_temperature}°C')
print(f'Current Wind Speed: {current_wind_speed} km/h')
else:
print('Error: Could not retrieve data for the specified city.')
#searching mekanizması, ve Crud operasyonu yapın.
import requests
def search_weather(city):
api_key = "key"
base_url = "http://api.weatherstack.com/current"
url = f'{base_url}?access_key={api_key}&query={city}'
response = requests.get(url)
data = response.json()
if response.status_code == 200 and 'current' in data:
location_name = data['location']['name']
current_temperature = data['current']['temperature']
current_wind_speed = data['current']['wind_speed']
print(f'City: {location_name}')
print(f'Current Temperature: {current_temperature}°C')
print(f'Current Wind Speed: {current_wind_speed} km/h')
else:
print(f'Error: Could not retrieve data for the city: {city}')
city = input('Enter the city name to search the weather: ')
search_weather(city)
import requests
def get_weather(city):
api_key = "key"
base_url = "http://api.weatherstack.com/current"
url = f'{base_url}?access_key={api_key}&query={city}'
response = requests.get(url)
data = response.json()
if response.status_code == 200 and 'current' in data:
location_name = data['location']['name']
current_temperature = data['current']['temperature']
current_wind_speed = data['current']['wind_speed']
print(f'City: {location_name}')
print(f'Current Temperature: {current_temperature}°C')
print(f'Current Wind Speed: {current_wind_speed} km/h')
else:
print(f'Error: Could not retrieve data for {city}')