您的位置:首页 > 编程语言 > Python开发

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

2008-04-11 11:22 471 查看
经常需要知道最近的天气情况,当然,目前有许多查看天气预报的方式,比如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()

 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  python import url input qq