从零开始用Python做一个女朋友 ? 第一弹( 二 )


点击图二右侧“测试”按钮可以进入测试页面,网页将模拟真实调用的情形对请求参数以及返回数据进行展示,为了辅助接下来的代码展示,先展示一下某次查询链接以及返回的json数据:
http://apis.juhe.cn/simpleWeather/query?city=%E9%95%BF%E6%B2%99&key=************// 注意此处的city参数必须为 utf8 urlencode 形式
{"reason":"查询成功","result":{"city":"长沙","realtime":{"temperature":"14","humidity":"73","info":"阴","wid":"02","direct":"西南风","power":"2级","aqi":"55"},"future":[{"date":"2020-02-22","temperature":"11\/18℃","weather":"多云","wid":{"day":"01","night":"01"},"direct":"北风转南风"},{"date":"2020-02-23","temperature":"11\/22℃","weather":"晴转多云","wid":{"day":"00","night":"01"},"direct":"南风"},{"date":"2020-02-24","temperature":"15\/26℃","weather":"多云","wid":{"day":"01","night":"01"},"direct":"南风"},{"date":"2020-02-25","temperature":"17\/25℃","weather":"阵雨","wid":{"day":"03","night":"03"},"direct":"南风"},{"date":"2020-02-26","temperature":"12\/23℃","weather":"阵雨","wid":{"day":"03","night":"03"},"direct":"北风"}]},"error_code":0}
然后接下来就是.py模块中文本分析函数()的实现了,为了方便阅读,此处将支持从城市关键词数组只展示一部分,所有支持查询的城市大家也可以在 [聚合数据] 上进行查询,该网站也提供了查询支持的城市的API:
def __analyse(raw_str):analyse_result = []weather_query_words = ['?', '怎么样', '如何']weather_talk_words = ['不错', '喜欢', '不喜欢']city_words = ['北京', '海淀', '朝阳', '顺义', '怀柔', '通州', '昌平', '延庆', '丰台', '石景山', '大兴', '房山', '密云', '门头沟', '平谷', '上海']#支持查询的城市关键词if any(word in raw_str for word in weather_query_words):query_props = {'time': 'TODAY', 'city': __weather_default_city}#如果未指定查询时间和城市则默认查询今天以及config.ini配置文件中城市的天气today_words = ['今天', 'today']#查询时间为“今天”的关键词tomorrow_words = ['明天', 'tomorrow']#查询时间为“明天”的关键词twodays_words = ['今天和明天', '今明']#查询时间为“今明两天”的关键词if any(word in raw_str for word in today_words):query_props['time'] = 'TODAY'if any(word in raw_str for word in tomorrow_words):query_props['time'] = 'TOMORROW'if any(word in raw_str for word in twodays_words):query_props['time'] = 'TWODAYS'for city in city_words:if city in raw_str:query_props['city'] = citybreakanalyse_result.append(Tag('weather-query', query_props))if any(word in raw_str for word in weather_talk_words):analyse_result.append(Tag('weather-talk', None))return analyse_result
在()调用分析之后,接下来就是()函数进行处理的部分了:
def handle(raw_str):analyse_result = __analyse(raw_str)#对主过程传入的raw_str进行关键词分析for tag in analyse_result:if tag.name == 'weather-talk':print('今天的天气真的很不错哦')if tag.name == 'weather-query':weather_complete_api = __weather_api_url + "?city=" + quote(tag.props["city"]) + "&key=" + __weather_api_key#调用quote函数将城市名转为 utf8 urlencode 形式并拼凑URLweather_response = json.loads(requests.get(weather_complete_api).content)#调用天气查询API并获取返回JSON格式的结果error_code = weather_response['error_code']#处理异常状态if (error_code == 207301 or error_code == 207302):print("Alice不知道这座城市是哪里的呢……要不主人再换一个试试?")elif (error_code == 207303):print("哔——电波在传输过程中好像出了点问题哎……")elif (error_code == 0):#错误码为0代表查询成功print("嘀哩哩~数据接收完成,下面为主人播报天气——")city = weather_response["result"]["city"]#返回的JSON内反馈的查询结果城市info_today = weather_response["result"]["future"][0]["weather"]#今日的天气状况temperature_today = weather_response["result"]["future"][0]["temperature"]#今天的最低气温和最高气温temperature_today_min = temperature_today[:temperature_today.find('/')]#处理temperature_today获得今日最低气温temperature_today_max = temperature_today[temperature_today.find('/') + 1:temperature_today.find('℃')]#处理temperature_today获得今日高气温info_tomorrow = weather_response["result"]["future"][1]["weather"]#明日天气状况temperature_tomorrow = weather_response["result"]["future"][1]["temperature"]#明日的最低气温和最高气温temperature_tomorrow_min = temperature_tomorrow[:temperature_tomorrow.find('/')]#处理temperature_today获得明日最低气温temperature_tomorrow_max = temperature_tomorrow[temperature_tomorrow.find('/') + 1:temperature_tomorrow.find('℃')]#处理temperature_today获得明日高气温wind_direction_today = weather_response["result"]["future"][0]["direct"]#今日风向wind_direction_tomorrow = weather_response["result"]["future"][1]["direct"]#明日风向wind_power_now = weather_response["result"]["realtime"]["power"]#当前时间风力大小#对标签内各属性值进行匹配处理if tag.props['time'] == 'TODAY':response_text = "今天[" + city + "]天气" + info_today + ",气温 " + temperature_today + ",今天风向为" + wind_direction_today + ",当前风力" + wind_power_now + " 。"if (int(temperature_today_max) - int(temperature_today_min) >= int(__temp_threshold)):response_text = response_text + "\n今天昼夜温差很大的说,主人要多加注意穿衣保暖哦~"if (int(wind_power_now[:-1]) >= int(__wind_threshold)):response_text = response_text + "\n另外,现在的风也有点大呢,主人一定当心不要被风吹跑啦,嘻嘻~"print(response_text)if tag.props['time'] == 'TOMORROW':response_text = "明天[" + city + "]天气" + info_tomorrow + ",气温 " + temperature_tomorrow + ",风向为" + wind_direction_tomorrow + " 。"if (int(temperature_tomorrow_max) - int(temperature_tomorrow_min) >= int(__temp_threshold)):response_text = response_text + "\n明天昼夜温差很大的说,主人要多加注意穿衣保暖哦~"print(response_text)if tag.props['time'] == 'TWODAYS':response_text = "今天[" + city + "]天气" + info_today + ",气温 " + temperature_today + ",今天风向为" + wind_direction_today + ",当前风力" + wind_power_now + ";\n明天天气" + info_tomorrow + ",气温 " + temperature_tomorrow + ",风向为" + wind_direction_tomorrow + " 。"if (int(temperature_today_max) - int(temperature_today_min) >= int(__temp_threshold) or int(temperature_tomorrow_max) - int(temperature_tomorrow_min) >= int(__temp_threshold)):response_text = response_text + "\n今明两天昼夜温差很大的说,主人要多加注意穿衣保暖哦~"if (int(wind_power_now[:-1]) >= int(__wind_threshold)):response_text = response_text + "\n另外,现在的风也有点大呢,主人一定当心不要被风吹跑啦,嘻嘻~"print(response_text)else:print("发生了Alice也不清楚的错误呢(?????)……要不主人帮忙看一下\n错误代码:[Weather-API:" + error_code + "]")#对于API的系统性错误直接抛出错误代码处理