Asp批量获取中央气象台数据库中城市数据,并存入数据库

中央气象台的天气预报API

http://www.weather.com.cn/data/sk/101221703.html
http://www.weather.com.cn/data/cityinfo/101221703.html
http://m.weather.com.cn/data/101221703.html

101221703即是城市在中央气象台数据库中城市码。

 

城市码获取步骤:

1. 通过 http://m.weather.com.cn/data5/city.xml 获取省份列表;
2. 通过 http://m.weather.com.cn/data5/city22.xml 获取该省份的城市列表,其中22为安徽省代号,1中所返回的;
3. 通过 http://m.weather.com.cn/data5/city2217.xml 获取该城市的区列表,其中2217为池州市代号,2中所返回的;
4. 通过 http://m.weather.com.cn/data5/city221703.xml 获取最后的城市码,其中221703为青阳县代号,3中所返回的;
5. 通过 api 获取天气信息。

 

ASP获取城市数据代码:
'获取省
url = "http://m.weather.com.cn/data5/city.xml"
rb = anhttp(url,"","GET")
Arr = split(rb,",")
for i = 0 to Ubound(Arr)
  pArr = split(Arr(i),"|")
  Response.Write("<br>"&pArr(1)&" - id:"&pArr(0)&" - pid:0<br>")
  'savedb pArr(0),pArr(1),"0"

  '获取市
  if isnumeric(pArr(0)) then
    url = "http://m.weather.com.cn/data5/city"&pArr(0)&".xml"
    rb = anhttp(url,"","GET")
    Arr2 = split(rb,",")
    for j = 0 to Ubound(Arr2)
      pArr2 = split(Arr2(j),"|")
      Response.Write(" -- "&pArr2(1)&" - id:"&pArr2(0)&" - pid:"&pArr(0)&"<br>")
      'savedb pArr2(0),pArr2(1),pArr(0)

      '获取区县
      if isnumeric(pArr2(0)) then
        url = "http://m.weather.com.cn/data5/city"&pArr2(0)&".xml"
        rb = anhttp(url,"","GET")
        Arr3 = split(rb,",")
        for k = 0 to Ubound(Arr3)
          pArr3 = split(Arr3(k),"|")
          Response.Write(" -- -- "&pArr3(1)&" - id:"&pArr3(0)&" - pid:"&pArr2(0)&"<br>")
          'savedb pArr3(0),pArr3(1),pArr2(0)
        next
      end if

    next
  end if

next

 

'存入数据库
sub savedb(cid,cname,pid)
  dim rs
  Set rs = ado_query_modify("select * from city")
  rs.addnew
  rs("cid") = cid
  rs("name") = cname
  rs("pid") = pid
  rs.update
  rs.Close
  Set rs = Nothing
end sub

 

其中函数anhttp()、ado_query_modify()请在本博客中找。

 

 

posted @ 2012-12-19 16:09  Al-one  阅读(455)  评论(0编辑  收藏  举报