python3通過json獲取天氣資訊

2021-08-31 04:00:22 字數 2051 閱讀 8941

通過python,每天定時傳送今明倆天的天氣資訊

測試的時候可以傳送給自己,後面可以傳送給指定的人或**

from __future__ import unicode_literals

from threading import timer

from wxpy import *

import requests

import urllib.request

import json

def get_weather(city_code):

url = "" + city_code

#print(url)

content = urllib.request.urlopen(url).read()

all_data =json.loads(content)

today = all_data['date']

#print(today)

city_info = all_data['cityinfo']

city = city_info['city']

#print(city)

data = all_data['data']

temperature = data['wendu']

#print(data['wendu'])

humidity = data['shidu']

#print(humidity)

air_quality = data['quality']

#print(air_quality)

forecast_info = data['forecast']

#print(forecast_info)

tomorrow_info = forecast_info[1]

#print(tomorrow_info)

tomorrow_date = tomorrow_info['date']

#print(tomorrow_date)

tomorrow_sunrise = tomorrow_info['sunrise']

#print(tomorrow_sunrise)

tomorrow_sunset = tomorrow_info['sunset']

#print(tomorrow_sunset)

tomorrow_date = tomorrow_info['date']

#print(tomorrow_date)

tomorrow_low = tomorrow_info['low']

#print(tomorrow_low)

tomorrow_high = tomorrow_info['high']

#print(tomorrow_high)

return ("日期:\n城市:\n溫度:\n濕度:\n 空氣質素:\n\

明天:\n明天氣溫:~\n明天日出:\n明天日落:"\

.format(today, city,temperature,humidity,air_quality,\

tomorrow_date,tomorrow_low,tomorrow_high,tomorrow_sunrise,tomorrow_sunset))

def send_news():

'''城市碼可以在網上查詢'''

codes = ["101280301","101280303","101280304","101280401","101280601"]

data = get_weather(codes[0])

bot = bot(cache_path=true)

'''這是是傳送給檔案助手,也可以傳送給指定的朋友或**'''

myself = bot.self

bot.file_helper.send(data)

'''一天86400秒,測試的時候可以改短'''

t = timer(86400, send_news)

t.start()

if __name__ == "__main__":

send_news()

python3通過pymongo操作mongoDB

2,增刪改查 mongodb預設開啟的埠號是27017 import pymongo 連線本地mongo服務 client bendi pymongo.mongoclient db bendi client bendi db name col bendi db bendi col name impo...

python3通過udp實現組播資料的傳送和接收

本文主要通過對海康攝像頭進行抓包,模擬傳送了udp包,並抓取攝像頭返回的資料報,解析並提取相關資訊。通過抓包發現,海康攝像頭傳送 接收資料使用udp協議,後來比較發現,使用python模擬起來比較簡單。由於攝像頭內建了udp協議的server端程式,本文主要使用python模擬客戶端傳送udp資料報...

Python3通過函式名呼叫函式的幾種場景實現

一 說明 除了執行系統命令外,我們有時還需要動態地執行一些python 有經驗的朋友就會知道可以使用內建函式eval實現這一需求,如eval print file 這還是比較簡單的。但如果要動態執行乙個函式,講的資料就會少一點,這次就要看這個需求該如何實現。二 通過eval實現 1 通過eval呼叫...