求助,怎么改才能在在腾讯云scf运行

jawy 8天前 6

求大神解决,哪个地方错了,怎么在SCF上报错,怎么修改呢???
[Python] 纯文本查看 复制代码
#!/usr/bin/python3# -*- coding: utf-8 -*-import requestsimport timefrom apscheduler.schedulers.blocking import BlockingSchedulerserver_url = "https://sc.ftqq.com/*******************.send"soup_url = "http://open.iciba.com/dsapi/"weather_url = "restapi.amap.com/v3/weather/weatherInfo?key=****************&city=*******"scheduler = BlockingScheduler()def get_time():    """    获取当前时间    """    return time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())def push_wx(text=None, desp=""):    """    推送消息到微信    """    params = {        "text": text,        "desp": desp    }    response = requests.get(server_url, params=params)    json_data = response.json()    if json_data['errno'] == 0:        print(get_time() + " 推送成功。")    else:        print("{0} 推送失败:{1} \n {2}".format(get_time(), json_data['errno'], json_data['errmsg']))def get_soup():    """    获取鸡汤    """    response = requests.get(soup_url)    json_data = response.json()    date = json_data['dateline']    content = json_data['content']    note = json_data['note']    picture = json_data['picture']    translation = json_data['translation']    return date, content, note, picture, translationdef get_weather():    """    获取天气    """    response = requests.get(weather_url)    json_data = response.json()    if json_data['status'] == '1':        return json_data['forecasts'][0]['casts'][0]    else:        print(get_time() + " 天气获取失败:" + json_data['info'])        return Nonedef make_soup():    """    制作鸡汤    """    soup = get_soup()    weather = get_weather()    if weather is None:        time.sleep(3)        weather = get_weather()    title = "早上好!"    desp = "#### {date}\n\n白天{dayweather},夜晚{nightweather},温度{nighttemp}℃ ~ {daytemp}℃。\n\n" \           "*{content}*\n\n{note}\n\n![]({picture})\n\n{translation}" \        .format(date=soup[0], dayweather=weather['dayweather'], nightweather=weather['nightweather'],                nighttemp=weather['nighttemp'], daytemp=weather['daytemp'],                content=soup[1],                note=soup[2],                picture=soup[3],                translation=soup[4])    push_wx(title, desp)if __name__ == '__main__':    print(get_time() + " 开始执行任务")    scheduler.add_job(make_soup, 'cron', day_of_week='0-6', minute=00, second=00)    scheduler.start()
最新回复 (15)
  • Zeaf 8天前
    引用 2
    jawy 发表于 2020-10-14 22:58
    那把有关这个删除了,定时就用个触发器,那该怎么改?

    我帮你改了个功能一样的[Python] 纯文本查看 复制代码
    # -*- coding: utf8 -*-import requestsdef main_handler(*arg):    def get_iciba_everyday():        icbapi = 'http://open.iciba.com/dsapi/'        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        #天气    key= 'https://sc.ftqq.com/屏蔽.send'      cpurl=key    api = 'http://t.weather.itboy.net/api/weather/city/'  # API地址,必须配合城市代码使用    city_code = '101010100'  # 进入[url=https://where.heweather.com/index.html]https://where.heweather.com/index.html[/url]查询你的城市代码    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"], "。")                    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,我感觉有用的我都弄进来了。        text="今日天气"        data={'text':text,'desp':tdwt.encode('UTF-8')}        res=requests.post(cpurl, data=data)  # 把天气数据转换成UTF-8格式,不然要报错。        print(res.text)    else:        error = '【出现错误】\n  今日天气推送错误,请检查服务状态!'        res=requests.post(cpurl, error.encode('utf-8'))        print(res.text)
  • Zeaf 8天前
    引用 3
    不知道这样可么
    [Python] 纯文本查看 复制代码
    # -*- coding: utf8 -*-import jsondef main_handler(event, context):    #!/usr/bin/python3    # -*- coding: utf-8 -*-        import requests    import time    from apscheduler.schedulers.blocking import BlockingScheduler        server_url = "https://sc.ftqq.com/*******************.send"    soup_url = "http://open.iciba.com/dsapi/"    weather_url = "restapi.amap.com/v3/weather/weatherInfo?key=****************&city=*******"        scheduler = BlockingScheduler()            def get_time():        """        获取当前时间        """        return time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())            def push_wx(text=None, desp=""):        """        推送消息到微信        """        params = {            "text": text,            "desp": desp        }            response = requests.get(server_url, params=params)        json_data = response.json()            if json_data['errno'] == 0:            print(get_time() + " 推送成功。")        else:            print("{0} 推送失败:{1} \n {2}".format(get_time(), json_data['errno'], json_data['errmsg']))            def get_soup():        """        获取鸡汤        """        response = requests.get(soup_url)        json_data = response.json()            date = json_data['dateline']        content = json_data['content']        note = json_data['note']        picture = json_data['picture']        translation = json_data['translation']            return date, content, note, picture, translation            def get_weather():        """        获取天气        """        response = requests.get(weather_url)        json_data = response.json()            if json_data['status'] == '1':            return json_data['forecasts'][0]['casts'][0]        else:            print(get_time() + " 天气获取失败:" + json_data['info'])            return None            def make_soup():        """        制作鸡汤        """        soup = get_soup()            weather = get_weather()            if weather is None:            time.sleep(3)            weather = get_weather()            title = "早上好!"        desp = "#### {date}\n\n白天{dayweather},夜晚{nightweather},温度{nighttemp}℃ ~ {daytemp}℃。\n\n" \            "*{content}*\n\n{note}\n\n![]({picture})\n\n{translation}" \            .format(date=soup[0], dayweather=weather['dayweather'], nightweather=weather['nightweather'],                    nighttemp=weather['nighttemp'], daytemp=weather['daytemp'],                    content=soup[1],                    note=soup[2],                    picture=soup[3],                    translation=soup[4])            push_wx(title, desp)            print(get_time() + " 开始执行任务")        scheduler.add_job(make_soup, 'cron', day_of_week='0-6', minute=00, second=00)        scheduler.start()
  • 楼主 jawy 8天前
    引用 4
    Zeaf 发表于 2020-10-14 21:21
    不知道这样可么
    [mw_shl_code=python,true]# -*- coding: utf8 -*-
    import json

    还是不行,而且server酱没有推送了
  • Zeaf 8天前
    引用 5
    jawy 发表于 2020-10-14 21:28
    还是不行,而且server酱没有推送了

    哦,缩进错了
  • Zeaf 8天前
    引用 6
    jawy 发表于 2020-10-14 21:28
    还是不行,而且server酱没有推送了

    [Python] 纯文本查看 复制代码
    # -*- coding: utf8 -*-import jsondef main_handler(event, context):    #!/usr/bin/python3    # -*- coding: utf-8 -*-        import requests    import time    from apscheduler.schedulers.blocking import BlockingScheduler        server_url = "https://sc.ftqq.com/*******************.send"    soup_url = "http://open.iciba.com/dsapi/"    weather_url = "restapi.amap.com/v3/weather/weatherInfo?key=****************&city=*******"        scheduler = BlockingScheduler()            def get_time():        """        获取当前时间        """        return time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())            def push_wx(text=None, desp=""):        """        推送消息到微信        """        params = {            "text": text,            "desp": desp        }            response = requests.get(server_url, params=params)        json_data = response.json()            if json_data['errno'] == 0:            print(get_time() + " 推送成功。")        else:            print("{0} 推送失败:{1} \n {2}".format(get_time(), json_data['errno'], json_data['errmsg']))            def get_soup():        """        获取鸡汤        """        response = requests.get(soup_url)        json_data = response.json()            date = json_data['dateline']        content = json_data['content']        note = json_data['note']        picture = json_data['picture']        translation = json_data['translation']            return date, content, note, picture, translation            def get_weather():        """        获取天气        """        response = requests.get(weather_url)        json_data = response.json()            if json_data['status'] == '1':            return json_data['forecasts'][0]['casts'][0]        else:            print(get_time() + " 天气获取失败:" + json_data['info'])            return None            def make_soup():        """        制作鸡汤        """        soup = get_soup()            weather = get_weather()            if weather is None:            time.sleep(3)            weather = get_weather()            title = "早上好!"        desp = "#### {date}\n\n白天{dayweather},夜晚{nightweather},温度{nighttemp}℃ ~ {daytemp}℃。\n\n" \            "*{content}*\n\n{note}\n\n![]({picture})\n\n{translation}" \            .format(date=soup[0], dayweather=weather['dayweather'], nightweather=weather['nightweather'],                    nighttemp=weather['nighttemp'], daytemp=weather['daytemp'],                    content=soup[1],                    note=soup[2],                    picture=soup[3],                    translation=soup[4])            push_wx(title, desp)        print(get_time() + " 开始执行任务")    scheduler.add_job(make_soup, 'cron', day_of_week='0-6', minute=00, second=00)    scheduler.start()
  • Zeaf 8天前
    引用 7
    报错的话把错误发一下
  • 楼主 jawy 8天前
    引用 8
    Zeaf 发表于 2020-10-14 21:33
    报错的话把错误发一下

  • Zeaf 8天前
    引用 9
    jawy 发表于 2020-10-14 22:14

    不如把你的key给我吧。。。
    图一没截全,只要图一的错误
  • 楼主 jawy 8天前
    引用 10
    Zeaf 发表于 2020-10-14 22:39
    不如把你的key给我吧。。。
    图一没截全,只要图一的错误

    [Python] 纯文本查看 复制代码
    # -*- coding: utf8 -*-import jsondef main_handler(event, context):# !/usr/bin/python3# -*- coding: utf-8 -*-    import requests    import time    from apscheduler.schedulers.blocking import BlockingScheduler    server_url = "https://sc.ftqq.com/SCU10152******************.send"    soup_url = "http://open.icib***********a.com/dsapi/"    weather_url = "restapi.amap.com/v3/weather/weatherInfo?key=8c****************7&city=11******0101"    scheduler = BlockingScheduler()    def get_time():        """        获取当前时间        """        return time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())    def push_wx(text=None, desp=""):        """        推送消息到微信        """        params = {            "text": text,            "desp": desp        }        response = requests.get(server_url, params=params)        json_data = response.json()        if json_data['errno'] == 0:            print(get_time() + " 推送成功。")        else:            print("{0} 推送失败:{1} \n {2}".format(get_time(), json_data['errno'], json_data['errmsg']))    def get_soup():        """        获取鸡汤        """        response = requests.get(soup_url)        json_data = response.json()        date = json_data['dateline']        content = json_data['content']        note = json_data['note']        picture = json_data['picture']        translation = json_data['translation']        return date, content, note, picture, translation    def get_weather():        """        获取天气        """        response = requests.get(weather_url)        json_data = response.json()        if json_data['status'] == '1':            return json_data['forecasts'][0]['casts'][0]        else:            print(get_time() + " 天气获取失败:" + json_data['info'])            return None    def make_soup():        """        制作鸡汤        """        soup = get_soup()        weather = get_weather()        if weather is None:            time.sleep(3)            weather = get_weather()        title = "早上好!"        desp = "#### {date}\n\n白天{dayweather},夜晚{nightweather},温度{nighttemp}℃ ~ {daytemp}℃。\n\n" \               "*{content}*\n\n{note}\n\n![]({picture})\n\n{translation}" \            .format(date=soup[0], dayweather=weather['dayweather'], nightweather=weather['nightweather'],                    nighttemp=weather['nighttemp'], daytemp=weather['daytemp'],                    content=soup[1],                    note=soup[2],                    picture=soup[3],                    translation=soup[4])        push_wx(title, desp)        print(get_time() + " 开始执行任务")        # scheduler.add_job(make_soup, 'cron', day_of_week='0-6', minute=00, second=00)        scheduler.start()
  • Zeaf 8天前
    引用 11
    from apscheduler.schedulers.blocking import BlockingScheduler

    这个库在云函数没有
  • 楼主 jawy 8天前
    引用 12
    Zeaf 发表于 2020-10-14 22:47
    这个库在云函数没有

    那把有关这个删除了,定时就用个触发器,那该怎么改?
  • 楼主 jawy 8天前
    引用 13
    Zeaf 发表于 2020-10-14 22:47
    这个库在云函数没有

    感谢大神
  • Zeaf 8天前
    引用 14
    我发现微信推送默认把换行变成了两个空格,导致排版不佳,得调调
    还有把自己的key隐藏一下,我先把我发的代码相关部分掩藏了
  • Zeaf 8天前
    引用 15
    改了下布局,把自己key填进去就可
    [Python] 纯文本查看 复制代码
    # -*- coding: utf8 -*-import requestsdef main_handler(*arg):    def get_iciba_everyday():        icbapi = 'http://open.iciba.com/dsapi/'        eed = requests.get(icbapi)        bee = eed.json()# 返回的数据        english = eed.json()['content']        zh_CN = eed.json()['note']        str = '\n\n【给小可爱的一句话】\n\n' + english + '\n\n' + zh_CN        return str        #天气    key= 'https://sc.ftqq.com/XXX.send'  #填自己的key    cpurl=key    api = 'http://t.weather.itboy.net/api/weather/city/'  # API地址,必须配合城市代码使用    city_code = '101010100'  # 进入[url=https://where.heweather.com/index.html]https://where.heweather.com/index.html[/url]查询你的城市代码    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"], "。")                    tdwt = '\n\n城市:' + d['cityInfo']['parent'] + ' ' + d['cityInfo']['city'] + '\n\n日期:' +  d["data"]["forecast"][0]["ymd"] + ' ' + d["data"]["forecast"][0]["week"] + '\n\n天气:' +  d["data"]["forecast"][0]["type"] + '\n\n温度:' + d["data"]["forecast"][0]["high"] + ' ' + d["data"]["forecast"][0]["low"] + '\n\n湿度:' + d["data"]["shidu"] + '\n\n空气质量:' + d["data"]["quality"] + '\n\n风力风向:' + d["data"]["forecast"][0]["fx"] + ' ' + d["data"]["forecast"][0]["fl"] + '\n\n温馨提示:' + d["data"]["forecast"][0]["notice"] + '。\n\n[更新时间:' + d["time"] + ']\n\n-----------------' + get_iciba_everyday()  # 天气提示内容,基本上该有的都做好了,如果要添加信息可以看上面的print,我感觉有用的我都弄进来了。        text="【今日份天气】"        data={'text':text,'desp':tdwt.encode('UTF-8')}        res=requests.post(cpurl, data=data)  # 把天气数据转换成UTF-8格式,不然要报错。        print(res.text)    else:        error = '【出现错误】\n  今日天气推送错误,请检查服务状态!'        res=requests.post(cpurl, error.encode('utf-8'))        print(res.text)
  • 楼主 jawy 7天前
    引用 16
    Zeaf 发表于 2020-10-14 23:10
    我发现微信推送默认把换行变成了两个空格,导致排版不佳,得调调
    还有把自己的key隐藏一下,我先把我发的 ...

    ok,谢谢大佬
  • 游客
    17
返回