用python写的抓取天气预报的脚本
2010-03-09 00:53
183 查看
用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,
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,
相关文章推荐
- 【详解】Python抓取数据脚本
- python网页爬虫之列车时刻表的抓取(4)-完整的python脚本
- Python抓取花瓣网图片脚本
- 使用Python3编写抓取网页和只抓网页图片的脚本
- 使用python脚本抓取百度fm中的歌曲
- python脚本抓取天气信息并发给微信好友
- [python]乱码:python抓取脚本
- Python脚本抓取采集小说网站
- 用Python BeautifulSoup写的一份多线程图片抓取的脚本
- 使用Python3编写抓取网页和只抓网页图片的脚本
- 编写Python脚本抓取网络小说来制作自己的阅读器
- 利用python脚本(re)抓取美空mm图片
- Python抓取Discuz!用户名脚本代码
- 利用python脚本抓取AC的代码[爬虫+HTMLParser+handle_entityref+正则表达式+模拟登陆+文件操作]
- 利用python脚本(xpath)抓取数据
- 24. Python脚本学习笔记二十四屏幕抓取
- python爬虫,抓取oracle-base上的一些常用脚本
- python学习笔记-抓取网页图片脚本
- python抓取网页图片的脚本