读取新浪天气预报信息
周末就要考试职称英语了,这个星期就好好做几套真题,最近手头没甚要紧事就把抓取天气预报的代码重新整了整,刚刚梳理了一下,先记录下来,随后再整合到我的weapho项目中。
抓取信息脚本:
#coding=utf-8 #file:sina_weather.py #by: fatway#gmail.com from urllib import urlencode, urlopen from utility import weather city_name = "东莞" result = [] base_url = "http://php.weather.sina.com.cn/xml.php" city = urlencode({"city":city_name.decode('utf-8').encode('gb2312')}) password = urlencode({"password":"DJOYnieT8234jlsK"}) for i in range(3): day = urlencode({"day": i}) reader = urlopen(base_url + "?" + "&".join((city, password, day))).read() result.append(reader) print u"今日天气:", weather(result[0]) print u"明天:", weather(result[1]) print u"后天:", weather(result[2])
通过解析新浪提供的xml文件来获取信息,解析xml文件的脚本如下:
#coding=utf-8 #file:utility.py #by: fatway#gmail.com from xml2dict import XML2Dict def _xml_2_dict(w_xml): xml = XML2Dict() w_dict = xml.fromstring(w_xml) return w_dict def _get_weather(w_xml): w_info = _xml_2_dict(w_xml) result = {} if 'Weather' not in w_info.Profiles: return result weather = w_info.Profiles.Weather city = weather.city # 城市 status1 = weather.status1 # 白天天气 status2 = weather.status2 # 夜间天气 power = weather.power1 # 风力 temperature1 = weather.temperature1 # 白天温度 temperature2 = weather.temperature2 # 夜间温度 result["city"] = city if status1 == status2: result["status"] = status1 else: result["status"] = status1 + u"\u8f6c" + status2 result["power"] = power result["temperature"] = str(temperature2) \ + "-" \ + str(temperature1) \ + u"\u2103" result["chy"] = chy return result def weather(w_xml): weather_info = _get_weather(w_xml) temp = [] # temp.append(weather_info["city"]) temp.append(weather_info["status"]) temp.append(weather_info["temperature"]) temp.append(weather_info["power"]) return ",".join(temp)
xml文件的解析我一直没有认真研究过,使用minidom就可以。为了省事我使用了开源项目xml2dict(http://code.google.com/p/xml2dict/),将xml文件转换为dict字典再处理。
PS-1:
备注一个准备用来做选择目标城市用的脚本
#coding=utf-8 #file:place.py #by: fatway#gmail.com province = ( (0, "北京"), (1, "天津"), (2, "重庆"), (3, "河北省"), (4, "山西省"), (5, "内蒙古"), (6, "辽宁省"), (7, "吉林省"), (8, "黑龙江省"), (9, "上海"), (10, "江苏省"), (11, "浙江省"), (12, "安徽省"), (13, "福建省"), (14, "江西省"), (15, "山东省"), (16, "河南省"), (17, "湖北省"), (18, "湖南省"), (19, "广东省"), (20, "广西"), (21, "海南省"), (22, "四川省"), (23, "贵州省"), (24, "云南省"), (25, "西藏"), (26, "陕西省"), (27, "甘肃省"), (28, "青海省"), (29, "宁夏"), (30, "新xj疆"), (31, "台湾省"), (32, "香港"), (33, "澳门") ) province_dict = dict(province) city = { 0: ((0, "北京"),), 1: ((0, "天津"),), 2: ((0, "重庆"),), 3: ((0, "石家庄"), (1, "唐山"), (2, "秦皇岛"), (3, "张家口"), (4, "承德"), (5, "廊坊"), (6, "邯郸"), (7, "邢台"), (8, "保定"), (9, "沧州"), (10, "衡水") ), 4: ((0, "太原"), (1, "大同"), (2, "阳泉"), (3, "晋城"), (4, "朔州"), (5, "忻州"), (6, "离石"), (7, "榆次"), (8, "临汾"), (9, "运城"), (10, "长治") ), 5: ((0, "呼和浩特"), (1, "包头"), (2, "乌海"), (3, "集宁"), (4, "巴彦浩特"), (5, "临河"), (6, "鄂尔多斯"), (7, "赤峰"), (8, "通辽"), (9, "锡林浩特"), (10, "海拉尔"), (11, "乌兰浩特") ), 6: ((0, "沈阳"), (1, "大连"), (2, "鞍山"), (3, "抚顺"), (4, "本溪"), (5, "锦州"), (6, "营口"), (7, "阜新"), (8, "盘锦"), (9, "铁岭"), (10, "朝阳"), (11, "丹东") ), 7: ((0, "长春"), (1, "吉林"), (2, "四平"), (3, "辽源"), (4, "松原"), (5, "白城"), (6, "延边"), (7, "通化") ), 8: ((0, "哈尔滨"), (1, "鸡西"), (2, "鹤岗"), (3, "双鸭山"), (4, "伊春"), (5, "佳木斯"), (6, "七台河"), (7, "牡丹江"), (8, "绥化"), (9, "齐齐哈尔"), (10, "大庆"), (11, "黑河"), (12, "大兴安岭") ), 9: ((0, "上海"),), 10: ((0, "南京"), (1, "无锡"), (2, "徐州"), (3, "常州"), (4, "苏州"), (5, "南通"), (6, "连云港"), (7, "淮阴"), (8, "盐城"), (9, "扬州"), (10, "镇江"), (11, "泰州"), (12, "宿迁") ), 11: ((0, "杭州"), (1, "宁波"), (2, "温州"), (3, "嘉兴"), (4, "湖州"), (5, "绍兴"), (6, "金华"), (7, "衢州"), (8, "舟山"), (9, "丽水"), (10, "台州") ), 12: ((0, "合肥"), (1, "芜湖"), (2, "蚌埠"), (3, "淮南"), (4, "马鞍山"), (5, "淮北"), (6, "铜陵"), (7, "安庆"), (8, "黄山市"), (9, "阜阳"), (10, "宿州"), (11, "滁州"), (12, "六安"), (13, "宣城"), (14, "巢湖"), (15, "池州") ), 13: ((0, "福州"), (1, "厦门"), (2, "莆田"), (3, "三明"), (4, "泉州"), (5, "漳州"), (6, "南平"), (7, "宁德"), (8, "龙岩") ), 14: ((0, "南昌"), (1, "景德镇"), (2, "赣州"), (3, "萍乡"), (4, "九江"), (5, "新余"), (6, "鹰潭"), (7, "宜春"), (8, "上饶"), (9, "吉安") ), 15: ((0, "济南"), (1, "青岛"), (2, "淄博"), (3, "枣庄"), (4, "东营"), (5, "烟台"), (6, "潍坊"), (7, "济宁"), (8, "泰安"), (9, "威海"), (10, "日照"), (11, "滨州"), (12, "德州"), (13, "聊城"), (14, "临沂"), (15, "菏泽"), (16, "莱芜") ), 16: ((0, "郑州"), (1, "开封"), (2, "洛阳"), (3, "平顶山"), (4, "安阳"), (5, "鹤壁"), (6, "新乡"), (7, "焦作"), (8, "濮阳"), (9, "许昌"), (10, "漯河"), (11, "三门峡"), (12, "商丘"), (13, "周口"), (14, "驻马店"), (15, "南阳"), (16, "信阳") ), 17: ((0, "武汉"), (1, "黄石"), (2, "十堰"), (3, "随州"), (4, "宜昌"), (5, "襄樊"), (6, "鄂州"), (7, "荆门"), (8, "黄冈"), (9, "孝感"), (10, "咸宁"), (11, "荆州"), (12, "恩施") ), 18: ((0, "长沙"), (1, "衡阳"), (2, "邵阳"), (3, "郴州"), (4, "永州"), (5, "韶山"), (6, "张家界"), (7, "怀化"), (8, "吉首"), (9, "株洲"), (10, "湘潭"), (11, "岳阳"), (12, "常德"), (13, "益阳"), (14, "娄底") ), 19: ((0, "广州"), (1, "佛山"), (2, "深圳"), (3, "汕尾"), (4, "惠州"), (5, "河源"), (6, "清远"), (7, "东莞"), (8, "珠海"), (9, "江门"), (10, "肇庆"), (11, "中山"), (12, "湛江"), (13, "茂名"), (14, "韶关"), (15, "汕头"), (16, "梅州"), (17, "阳江"), (18, "潮州"), (19, "顺德"), (20, "揭阳"), (21, "云浮") ), 20: ((0, "南宁"), (1, "梧州"), (2, "玉林"), (3, "桂林"), (4, "百色"), (5, "河池"), (6, "钦州"), (7, "柳州"), (8, "北海"), (9, "防城港"), (10, "贵港"), (11, "贺州") ), 21: ((0, "海口"), (1, "三亚"), (2, "西沙") ), 22: ((0, "成都"), (1, "眉山"), (2, "雅安"), (3, "峨嵋山"), (4, "自贡") ), 23: ((0, "贵阳"), (1, "六盘水"), (2, "铜仁"), (3, "安顺"), (4, "凯里"), (5, "都匀"), (6, "兴义"), (7, "毕节"), (8, "遵义") ), 24: ((0, "昆明"), (1, "德宏"), (2, "曲靖"), (3, "楚雄"), (4, "玉溪"), (5, "红河"), (6, "文山"), (7, "思茅"), (8, "昭通"), (9, "西双版纳"), (10, "大理"), (11, "保山"), (12, "怒江"), (13, "丽江"), (14, "迪庆"), (15, "临沧") ), 25: ((0, "拉萨"), (1, "昌都"), (2, "日喀则"), (3, "那曲"), (4, "阿里"), (5, "林芝") ), 26: ((0, "西安"), (1, "铜川"), (2, "宝鸡"), (3, "咸阳"), (4, "渭南"), (5, "汉中"), (6, "安康"), (7, "商洛"), (8, "延安"), (9, "榆林") ), 27: ((0, "兰州"), (1, "白银"), (2, "金昌"), (3, "天水"), (4, "张掖"), (5, "武威"), (6, "定西"), (7, "平凉"), (8, "临夏"), (9, "嘉峪关"), (10, "酒泉") ), 28: ((0, "西宁"), (1, "果洛"), (2, "海西"), (3, "格尔木"), (4, "海东"), (5, "海北"), (6, "玉树"), (7, "黄南") ), 29: ((0, "银川"), (1, "石嘴山"), (2, "吴忠"), (3, "固原") ), 30: ((0, "乌鲁木齐"), (1, "克拉玛依"), (2, "吐鲁番"), (3, "哈密"), (4, "昌吉"), (5, "博乐"), (6, "库尔勒"), (7, "阿克苏"), (8, "克州"), (9, "喀什"), (10, "伊犁"), (11, "石河子"), (12, "塔城"), (13, "阿勒泰") ), 31: ((0, "台北"),), 32: ((0, "香港"),), 33: ((0, "澳门"),) } def get_all_province_name(): '''返回所有省份名称列表''' return province_dict.values() def get_province_name_by_id(prov_id): '''根据省份代码获取名称''' id = int(prov_id) name = province_dict.get(id) return name def get_province_id_by_name(prov_name): '''根据省份名称获取省份代码''' for p_id,p_name in province: if prov_name == p_name: return p_id return 99 def get_citys_by_province_id(prov_id): '''根据省份代码获取所属城市清单''' prov_id = int(prov_id) return dict(city.get(prov_id, ())) def get_citys_by_province_name(prov_name): '''根据省份名称获取所属城市清单''' prov_id = get_province_id_by_name(prov_name) return get_citys_by_province_id(prov_id) def get_city_name_by_provandcity_id(prov_id,city_id): '''根据省、市代码获取城市名称''' prov_id,city_id = int(prov_id),int(city_id) citys_in_prov = get_citys_by_province_id(prov_id) city_name = citys_in_prov.get(city_id, ()) return city_name def get_id_by_city_name(city_name): '''根据城市名称查询对应的省、市代码''' for p_id,p_name in province: citys = get_citys_by_province_id(p_id) for c_id,c_name in citys.items(): if city_name == c_name: return p_id,c_id return 99,99 def city_viewer(): '''获取省份和城市的全景字典''' result = {} for prov_key,prov_name in province: sub_result = {} sub_result[prov_name] = dict(city.get(prov_key, ())).values() result.update(sub_result) return result if __name__ == "__main__": print get_province_name_by_id(19).decode('utf-8').encode('gbk') print get_province_id_by_name("广东省") print get_city_name_by_provandcity_id(19, 07).decode('utf-8').encode('gbk') print get_id_by_city_name("东莞")
PS-2(废话):
中午购网线30米、天线35米、转接头3个、网线直通一个、水晶头若干,花掉125大元。下午肯定要在困顿中度过了。
-End-