本帖最后由 zhangyongkang 于 2021-4-13 14:26 编辑
[Python] 纯文本查看 复制代码 #!/usr/bin/python3
#coding=utf-8
import requests, json
import time
spkey = "**" #key
def get_iciba_everyday():
icbapi = 'https://v2.alapi.cn/api/qinghua?token=**'
eed = requests.get(icbapi)
bee = eed.json() #返回的数据
english = eed.json()['content']
zh_CN = eed.json()['note']
str = '\n【奇怪的知识】\n' + english + '\n' + zh_CN
return str
print(get_iciba_everyday())
def main(*args):
api ='http://t.weather.itboy.net/api/weather/city/'#[backcolor=rgb(255, 255, 254)]API地址,必须配合城市代码使用[/backcolor]
city_code = '101210101' #查询你的城市代码
tqurl = api + city_code
response = requests.get(tqurl)
d = response.json() #将数据以json形式返回,这个d就是返回的json数据
if(d['status'] == 200): #当返回状态码为200,输出天气状况
print("城市:",d["cityInfo"]["parent"], d["cityInfo"]["city"])
print("更新时间:",d["time"])
print("日期:",d["data"]["forecast"][0]["ymd"])
print("星期:",d["data"]["forecast"][0]["week"])
print("天气:",d["data"]["forecast"][0]["type"])
print("温度:",d["data"]["forecast"][0]["high"],d["data"]["forecast"][0]["low"])
print("湿度:",d["data"]["shidu"])
print("PM25:",d["data"]["pm25"])
print("PM10:",d["data"]["pm10"])
print("空气质量:",d["data"]["quality"])
print("风力风向:",d["data"]["forecast"][0]["fx"],d["data"]["forecast"][0]["fl"])
print("感冒指数:",d["data"]["ganmao"])
print("温馨提示:",d["data"]["forecast"][0]["notice"],"。")
cpurl = 'https://qmsg.zendee.cn/send/'+spkey #自己改发送方式,我专门创建了个群来收消息,所以我用的group
tdwt = '【今日份天气】\n城市:'+d['cityInfo']['parent']+' '+d['cityInfo']['city']+'\n日期:'+d["data"]["forecast"][0]["ymd"]+' '+d["data"]["forecast"][0]["week"]+'\n天气:'+d["data"]["forecast"][0]["type"]+'\n温度:'+d["data"]["forecast"][0]["high"]+' '+d["data"]["forecast"][0]["low"]+'\n湿度:'+d["data"]["shidu"]+'\n空气质量:'+d["data"]["quality"]+'\n风力风向:'+d["data"]["forecast"][0]["fx"]+' '+d["data"]["forecast"][0]["fl"]+'\n温馨提示:'+d["data"]["forecast"][0]["notice"]+'。\n[更新时间:'+d["time"]+']\n?-----------------'+get_iciba_everyday() #天气提示内容,基本上该有的都做好了,如果要添加信息可以看上面的print,我感觉有用的我都弄进来了。
requests.post(cpurl,tdwt.encode('utf-8')) #把天气数据转换成UTF-8格式,不然要报错。
else:
error = '【出现错误】\n 今日天气推送错误,请检查服务状态!'
requests.post(cpurl,error.encode('utf-8'))
def main_handler(event, context):
return main()
if __name__ == '__main__':
main()
报错
[Python] 纯文本查看 复制代码 {
"errorCode": -1,
"errorMessage": "Traceback (most recent call last):\n File \"/var/runtime/python3/bootstrap.py\", line 133, in init_handler\n func_handler = get_func_handler(file.rsplit(\".\", 1)[0], func)\n File \"/var/runtime/python3/bootstrap.py\", line 159, in get_func_handler\n mod = imp.load_module(mname, *imp.find_module(mname))\n File \"/var/lang/python3/lib/python3.6/imp.py\", line 234, in load_module\n return load_source(name, filename, file)\n File \"/var/lang/python3/lib/python3.6/imp.py\", line 172, in load_source\n module = _load(spec)\n File \"Ϗrozen; importlib._bootstrap>\", line 675, in _load\n File \"Ϗrozen; importlib._bootstrap>\", line 655, in _load_unlocked\n File \"Ϗrozen; importlib._bootstrap_external>\", line 678, in exec_module\n File \"Ϗrozen; importlib._bootstrap>\", line 205, in _call_with_frames_removed\n File \"/var/user/index.py\", line 16, in <module;>\n print(get_iciba_everyday())\n File \"/var/user/index.py\", line 11, in get_iciba_everyday\n eed = eed.json() #返回的数据\nUnboundLocalError: local variable 'eed' referenced before assignment",
"requestId": "4155a14a-abad-441a-b121-5b8210f93d82",
"statusCode": 443
} |