Python Scripts: Difference between revisions

Line 923:
root.geometry("400x200")
root.mainloop()
</syntaxhighlight>
 
 
= Weather Parcer =
<syntaxhighlight lang="py
import socket
import json
 
s = socket.socket()
addr = socket.getaddrinfo('api.openweathermap.org', 80)
 
s.connect(addr[0][4])
s.send(b'GET http://api.openweathermap.org/data/2.5/weather?q=Bangalore&appid={key} HTTP/1.0\r\n\r\n')
 
html = s.recv(1000)
div = html.split(b'\r\n\r\n')[-1]
data = json.loads(div)
 
main = data['weather'][0]['main']
print("Main =", main)
 
desc = data['weather'][0]['description']
print("Description =", desc)
 
Celcius = data['main']['temp'] - 273.15
rnd = str(round(Celcius,2))
print("Temperature =", rnd, "\xb0C")
 
pres = data['main']['pressure']
print("Pressure =", pres, "mbar")
 
hum = data['main']['humidity']
print("Humidity =", hum, "%")
 
vis = int(data['visibility'])/1000
print("Visibility =", vis, "Km?")
 
speed = data['wind']['speed']
try:
dir = data['wind']['deg']
except KeyError:
dir = 0
rnd2 = str(round(dir,2))
print("Wind Speed =", speed, "Km/h?")
print("Wind Direction =", rnd2, "Degree")
</syntaxhighlight>