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

Python取得天气预报的一个例子(南京)

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

经常需要知道最近的天气情况,当然,目前有许多查看天气预报的方式,比如QQ天气等,不过我们也可以自已写一个,体验一下DIY的乐趣,呵呵。本文主要介绍的是使用Python的urllib从天气预报网站抓取预报内容的方法,如果你在南京,那下面的代码可以直接使用(除非抓取的网站改版)。

废话少说,上代码:

# -*- coding: utf-8 -*-
import urllib
import re

def getWF():
    url 
= "http://www.jlweather.com/nanjing_map.asp"
    
print "] downloading the page from", url
    u 
= urllib.urlopen(url)
    page_content 
= ""
    
while 1:
        line 
= u.readline()
        
if len(line) == 0:
            
break
        page_content 
+= line
    u.close()
    
print "] page has been downloaded, analyse starting ..."
    idx 
= page_content.index("南京市气象局今天")
    p 
= page_content[idx:]
    idx 
= p.index("</font>")
    p 
= p[:idx]
    p 
= re.sub(r" +""", p)
    
print
    
print p

    f 
= open("weatherForecast_NJ.txt""w+")
    f.write(p)
    f.close()

if __name__ == "__main__":
    
print "] Show the last report saved in local device."
    
try:
        f 
= open("weatherForecast_NJ.txt""r")
        s 
= f.read()
        
print
        
print s
        f.close()
    
except:
        
print "] no report file is found here!"
    
print
    
print "] Now get the weather forecast of Nanjing."
    getWF()
    
print "] done!"
    
#print "] press Enter to quite.",
    #raw_input()

 

抱歉!评论已关闭.