现在的位置: 首页 > 综合 > 正文

使用python调用web service抓取天气预报

2013年02月02日 ⁄ 综合 ⁄ 共 3572字 ⁄ 字号 评论关闭

网上找到落落的抓取天气预报的代码,觉的简单易用,修改一下,放在后勤首页中。Webservice从

http://www.webxml.com.cn/webservices/weatherwebservice.asmx获取

1、主要代码

#! /usr/bin/env python 
#_*_coding:utf-8_*_ 
import urllib 
page = urllib.urlopen("http://www.webxml.com.cn/webservices/weatherwebservice.asmx/getWeatherbyCityName?theCityName=青岛"); 
body = page.readlines(); 
page.close(); 
for line in body: 
  print line

不过它返回回来的是一段xml字符串

<ArrayOfString>
<string>山东</string>
<string>青岛</string>
<string>54857</string>
<string>54857.jpg</string>
<string>2011-4-1 8:59:53</string>
<string>3℃/14℃</string>
<string>4月1日 多云转阴</string>
<string>南风4-5级转北风5-6级</string>
<string>1.gif</string>
<string>2.gif</string>
<string>今日天气实况:气温:8℃;风向/风力:南风 4级;湿度:79%;空气质量:良;紫外线强度:最弱</string>

<string>
穿衣指数:天气凉,建议着厚外套加毛衣等春秋服装。体弱者宜着大衣、呢外套。因昼夜温差较大,注意增减衣服。
感冒指数:昼夜温差极大,且风力较强,极易发生感冒,请特别注意增减衣服保暖防寒。
运动指数:天气较好,请减少运动时间并降低运动强度,因紫外线强且风力较大,户外运动避风防晒。
洗车指数:较适宜洗车,未来持续两天无雨,但考虑风力较大,擦洗一新的汽车会蒙上灰尘。
晾晒指数:天气晴朗,午后温暖的阳光仍能满足你驱潮消霉杀菌的晾晒需求。
旅游指数:白云飘飘,温度适宜,但风稍微有点大。这样的天气很适宜旅游,您可以尽情地享受大自然的无限风光。
路况指数:晴天,其它条件适宜,路面比较干燥,路况较好。
舒适度指数:白天天气晴好、但风力较强,早晚会感觉偏凉,午后舒适、宜人。
</string>
<string>2℃/11℃</string>
<string>4月2日 阴转多云</string>
<string>北风5-6级转4-5级</string>
<string>2.gif</string>
<string>1.gif</string>
<string>5℃/14℃</string>
<string>4月3日 晴转多云</string>
<string>北风3-4级转南风3-4级</string>
<string>0.gif</string>
<string>1.gif</string>

<string>
青岛市地处山东半岛南部,位于东经119°30′至121°00′,北纬35°35′至37°09′,东、南濒临黄海,东北与烟台市毗邻,西与潍坊市相连,西南与日照市接壤。全市总面积为东、南濒临黄海,东北与烟台市毗邻,西与潍坊市相连,西南与日照市接壤。面积:总面积10654.1平方公里。人口:人口700多万。青岛气候有着鲜明的特点,那就是:四季分明,夏短冬长;夏无酷暑,冬少严寒;降水适中,热量充足;春夏多雾,冬春风大。青岛夏季较内地短,一年平均只有80天,夏季平均气温为23摄氏度,最热的8月平均气温为25.1摄氏度,由于受海洋影响,比较凉爽,是人们避暑,疗养和游泳的最好季节。青岛近代历经沧桑,有着丰富的文化旅游景观,根据景点的分布和文化内涵,可分为各具特色的四大区域,即:西部旧城区(西方近代城市风貌)、东部新区(现代化国际城市风貌)、市区腹地(胶东民俗文化风貌)、郊区(历代文物古迹风貌)。到青岛来游览,即可享受宜人的气候和优美的风光。又可在观赏中外文化碰撞交融的结晶中产生深层次的思索和启迪,因而具有很高的旅游价值。景观:趵崂山、八大关、栈桥、五四广场。
</string>
</ArrayOfString>

2、返回回来的是一段xml文档,现在就是解析xml文档获得我需要的数据

使用到的类 xml.dom.minidom ,

API

parse( filename_or_file, parser)
Return a Document from the given input. filename_or_file may be either a file name, or a file-like object. parser, if given, must be a SAX2 parser object. This function
will change the document handler of the parser and activate namespace support; other parser configuration (like setting an entity resolver) must have been done in advance.

If you have XML in a string, you can use the parseString() function instead:

 

parseString( string[, parser])
Return a Document that represents the string. This method creates a StringIO object for the string and passes that on to parse

这两个方法返回的都是Document对象,看看Document常用的几个方法:

createElement_x(tagName):创建并返回一个新的Element节点

createTextNode(data)添加一个的文本节点

createComment(data)添加注释

createAttribute(name)添加属性

getElementsByTagName_r(tagName)

这里使用parseString(string)这个方法

#! /usr/bin/env python

#_*_coding:utf-8_*_
import urllib
from xml.dom.minidom import parseString

def weather():
  page = urllib.urlopen("http://www.webxml.com.cn/webservices/weatherwebservice.asmx/getWeatherbyCityName?theCityName=青岛");
  body = page.readlines();
  page.close();

  #body是一个list,需要转成string
  document=""
  for line in body:
    document = document + line
  #print document

  dom =parseString(document)

  strings = dom.getElementsByTagName_r("string")
  #今天的温度和天气
  today_temperature=getText(strings[5].childNodes)
  today_weather=getText(strings[6].childNodes)

  #明天的温度和天气

  tomorrow_weather=getText(strings[13].childNodes)
  tomorrow_temperature=getText(strings[12].childNodes)

  weather=today_weather+" " +today_temperature+" ;  " +tomorrow_weather+" "+tomorrow_temperature

  return weather

def getText(nodelist):
    rc = ""
    for node in nodelist:
        if node.nodeType == node.TEXT_NODE:
            rc = rc + node.data
    return rc

if __name__=="__main__":
  weather = weather()
  print weather

输出结果为:

>>> 4月1日 多云转阴 3℃/14℃   4月2日
阴转多云 2℃/11℃

抱歉!评论已关闭.