腾讯云函数调用天气Api报错求python大佬帮看看咋回事
本帖最后由 zhangyongkang 于 2021-4-13 14:26 编辑#!/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/'#API地址,必须配合城市代码使用
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"]["ymd"])
print("星期:",d["data"]["forecast"]["week"])
print("天气:",d["data"]["forecast"]["type"])
print("温度:",d["data"]["forecast"]["high"],d["data"]["forecast"]["low"])
print("湿度:",d["data"]["shidu"])
print("PM25:",d["data"]["pm25"])
print("PM10:",d["data"]["pm10"])
print("空气质量:",d["data"]["quality"])
print("风力风向:",d["data"]["forecast"]["fx"],d["data"]["forecast"]["fl"])
print("感冒指数:",d["data"]["ganmao"])
print("温馨提示:",d["data"]["forecast"]["notice"],"。")
cpurl = 'https://qmsg.zendee.cn/send/'+spkey #自己改发送方式,我专门创建了个群来收消息,所以我用的group
tdwt = '【今日份天气】\n城市:'+d['cityInfo']['parent']+' '+d['cityInfo']['city']+'\n日期:'+d["data"]["forecast"]["ymd"]+' '+d["data"]["forecast"]["week"]+'\n天气:'+d["data"]["forecast"]["type"]+'\n温度:'+d["data"]["forecast"]["high"]+' '+d["data"]["forecast"]["low"]+'\n湿度:'+d["data"]["shidu"]+'\n空气质量:'+d["data"]["quality"]+'\n风力风向:'+d["data"]["forecast"]["fx"]+' '+d["data"]["forecast"]["fl"]+'\n温馨提示:'+d["data"]["forecast"]["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()
报错
{
"errorCode": -1,
"errorMessage": "Traceback (most recent call last):\nFile \"/var/runtime/python3/bootstrap.py\", line 133, in init_handler\n func_handler = get_func_handler(file.rsplit(\".\", 1), func)\nFile \"/var/runtime/python3/bootstrap.py\", line 159, in get_func_handler\n mod = imp.load_module(mname, *imp.find_module(mname))\nFile \"/var/lang/python3/lib/python3.6/imp.py\", line 234, in load_module\n return load_source(name, filename, file)\nFile \"/var/lang/python3/lib/python3.6/imp.py\", line 172, in load_source\n module = _load(spec)\nFile \"Ϗrozen; importlib._bootstrap>\", line 675, in _load\nFile \"Ϗrozen; importlib._bootstrap>\", line 655, in _load_unlocked\nFile \"Ϗrozen; importlib._bootstrap_external>\", line 678, in exec_module\nFile \"Ϗrozen; importlib._bootstrap>\", line 205, in _call_with_frames_removed\nFile \"/var/user/index.py\", line 16, in <module;>\n print(get_iciba_everyday())\nFile \"/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
} Token错误啊 老铁 城市code从哪里获取的 gentlespider 发表于 2021-4-13 14:28
Token错误啊
token没有错 我的postman能请求到数据 zhoukaiquan 发表于 2021-4-13 14:31
老铁 城市code从哪里获取的
和风天气APihttps://where.qweather.com/index.html {:301_996:}我直接用的百度搜索天气的api 闷骚小贱男 发表于 2021-4-13 14:34
我直接用的百度搜索天气的api
我这是用qmsg酱 整到手机上的 local variable 'eed' referenced before assignment,你定义eed的位置不对吧 如果,学习一下 zhangyongkang 发表于 2021-4-13 14:36
我这是用qmsg酱 整到手机上的
我知道呀///我是说我的api来自https://www.baidu.com/s?wd=%E5%A4%A9%E6%B0%94&ie=UTF-8