环境:win7,python3.7
安装好 ichat,requests,yaml,Schedule(定时功能使用)
准备一份全国地区的code;参照图一:citys.yml
需先了解全国天气相关接口API:
思路:想通过输入一个城市名就可以打印出相关天气信息;
由于yml配置文件只有城市名:城市代码code
通过城市名去获取code,在把code 引用到URL路径上进行对每个城市的天气信息进行爬取
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from threading import Timer
from itchat.content import *
import requests
import itchat
import yaml
#调用城市名,返回对应的城市code,通过yaml模块对yml配置文件进行解析
def getCityArea(cityName):
with open("citys.yml",'r',encoding = "utf-8") as f:
city_cfg = f.read() #返回的是一大串字符串
city_dic = yaml.load(city_cfg) #需要再次转换成字典格式
#print(city_cfg)
#print(city_dic)
f.close() # 关闭文件操作
#获取城市对应的CodeID
city_ID = city_dic['city'][cityName]
#print(city_ID)
return city_ID #返回ID
#获取每日更新天气
def getWeather(cityID):
#打开全国天气API
url = r"