
04.09.2021, 14:49
|
|
Флудер
Регистрация: 10.10.2016
Сообщений: 9,228
С нами:
5046982
Репутация:
183
|
|
привет, пару часов назад захотел начать учить пайтон a.k.a питон и в качестве первой работы решил сделать бота для тг который будет отправлять погоду, но при обращении к API сайта мне выдает как я понял json таблицу, как можно достать из нее определенные значения?
res.text:
Код:
Погода
:
{
"coord"
:
{
"lon"
:
37.6156
,
"lat"
:
55.7522
}
,
"weather"
:
[
{
"id"
:
804
,
"main"
:
"Clouds"
,
"description"
:
"overcast clouds"
,
"icon"
:
"04d"
}
]
,
"base"
:
"stations"
,
"main"
:
{
"temp"
:
10.17
,
"feels_like"
:
8.7
,
"temp_min"
:
9.75
,
"temp_max"
:
10.29
,
"pressure"
:
1007
,
"humidity"
:
56
,
"sea_level"
:
1007
,
"grnd_level"
:
989
}
,
"visibility"
:
10000
,
"wind"
:
{
"speed"
:
6.55
,
"deg"
:
296
,
"gust"
:
9.42
}
,
"clouds"
:
{
"all"
:
98
}
,
"dt"
:
1630752048
,
"sys"
:
{
"type"
:
2
,
"id"
:
2009195
,
"country"
:
"RU"
,
"sunrise"
:
1630723217
,
"sunset"
:
1630772239
}
,
"timezone"
:
10800
,
"id"
:
524901
,
"name"
:
"Moscow"
,
"cod"
:
200
}
python:
Код:
import
telebot
import
requests
cityid
=
'524901'
## Москва
,
узнать id
:
https
:
/
/
openweathermap
.
org
/
city
/
524901
api_url
=
"http://api.openweathermap.org/data/2.5/weather?"
cf
=
'metric'
apikey
=
'1c8790deb22eccba1aba3596a53b275d'
weatherPlaceHolder
=
'text +1'
print
(
"BOT STARTED----------------------------------"
)
bot
=
telebot
.
TeleBot
(
'huypizda'
)
@bot
.
message_handler
(
commands
=
[
'start'
]
)
def
start_command
(
message
)
:
bot
.
send_message
(
message
.
chat
.
id
,
"Привет, ты запустил бота Chapo's Weather Bot, введи /get если хочешь узнать погоду"
)
@bot
.
message_handler
(
commands
=
[
'get'
]
)
def
start_command
(
message
)
:
res
=
requests
.
get
(
api_url
+
'id='
+
cityid
+
'&units='
+
cf
+
'&APPID='
+
apikey
)
bot
.
send_message
(
message
.
chat
.
id
,
"Погода: "
+
res
.
text
)
bot
.
polling
(
)
##
FROM
LUA
## api_url
=
"http://api.openweathermap.org/data/2.5/weather?"
##
--
http
:
/
/
openweathermap
.
org
/
help
/
city_list
.
txt
,
http
:
/
/
openweathermap
.
org
/
find
## cityid
=
"5128581"
## cf
=
"metric"
--
metric or imperial
##
## apikey
=
"1c8790deb22eccba1aba3596a53b275d"
--
get
an open weather map api key
:
http
:
/
/
openweathermap
.
org
/
appid
## measure
=
'°'
.
.
(
cf
==
'metric'
and
'C'
or
'F'
)
--
measure is °
C
if
metric and °
F
if
imperial
## weather
=
http
.
request
(
(
"%sid=%s&units=%s&APPID=%s"
)
:
format
(
api_url
,
cityid
,
cf
,
apikey
)
)
|
|
|
|
Предыдущая тема
Следующая тема
|
Здесь присутствуют: 1 (пользователей: 0 , гостей: 1)
|
|
|
|