用python寫的抓取天氣預報的腳本

用python寫的抓取天氣預報的腳本

http://blog.chinaunix.net/u2/82009/showart_2166843.html

 

從昨天開始的看關於網絡抓取的東西,而且自己的用的是awesome ,所以寫了這個天氣預報的腳本給我的awesome,這個天氣腳本直接取下來的話是七天的天氣預報從中國天氣網上,我後面對它做了處理,用到了我的awesome上
效果:1日星期一夜間 陰 低溫 4℃ 無持續風向 微風 | 2日星期二 小雨 --> 雨夾雪 3℃ --> 6℃ | 3日星期三 雨夾雪 1℃ --> 5℃
我只取了三天的預報,三天已經夠了,下面程序的註釋 英文實在有點過不了關

================================================

#!/usr/bin/env python
# weather html parser

from HTMLParser import HTMLParser
import sys,urllib2,string,re

# define a class to parser a html
class HtmlParser(HTMLParser):
    def __init__(self):
        self.data=''
        self.readingdata=0
        HTMLParser.__init__(self)

    def handle_starttag(self,tag,attrs):
        if tag == 'td':
            self.readingdata=1
    def handle_data(self,chars):
        if self.readingdata:
            self.data+=chars

    def handle_endtag(self,tag):
        if tag=='td':
            self.readingdata=0
    def cleanse(self):
        self.data = re.sub('/s+',' ', self.data)
    def getdata(self):
        self.cleanse()
        return self.data

# this url is a place where you want to know the weather forecast
url="http://www.weather.com.cn/html/weather/101210501.shtml"
req=urllib2.Request(url)
fd=urllib2.urlopen(req)
tp=HtmlParser()
tp.feed(fd.read())
weather=tp.getdata()
# when you are getting a weather after parsering
# this weather string have 7 days weather forecast
# the following if for my awesome format
weather=weather.split()
tag=[weather.index(i) for i in weather if '/xe6/x97/xa5' in i]
first=weather[:tag[1]]
second=weather[tag[1]:tag[2]]
if second[1]!=second[7]:second[1]+=' --> '+second[7]
second[2]=second[9]+' --> '+second[3]
second[0]=second[0][:-6]
second=second[:3]
third=weather[tag[2]:tag[3]]
if third[1]!=third[7]:third[1]+=' --> '+third[7]
third[2]=third[9]+' --> '+third[3]
third[0]=third[0][:-6]
third=third[:3]
weather=['<span color="green">    Weather:</span>']+first+['|']+second+['|']+third
for i in weather:print i,

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章