python use BeautifulSoup crawl weather forecast

作者: 2hei 发表于2009年9月16日 18:05
版权声明: 可以转载, 转载时务必以超链形式标明文章原始出处和作者信息及版权声明
http://www.2hei.net/mt/2009/09/python-use-beautifulsoup-crawl-weather-forecast.html
python 使用BeautifulSoup(美汤)来抓取天气预报,简单的例子,只当练手

#!/usr/bin/env python
# -*- coding: gb2312 -*-

#get weather info from internet
#http://weather.china.com.cn/city/54511_full.html

import urllib,re,unicodedata,string,sys,re
from BeautifulSoup import *

if __name__=="__main__":
    response = urllib.urlopen("http://weather.china.com.cn/city/54511_full.html")
    result = response.read()
    soup = BeautifulSoup(''.join(result))
    weather = soup.findAll('td')
    list = []
    tmplist = []
    for i in xrange(0,len(weather)):
        tmpstr = weather[i].string
        if tmpstr not in [' ',None,u'地方气象网站','合作伙伴:中国气象局中央气象台',u'天气预报']:
            list.append(tmpstr)
    for j in xrange(0,4):
        print list[j]


result is:
北京
气温:29 ℃~17 ℃
风向:微风
风力:小于3 级
| | Comments (0) | TrackBacks (0)

发表一个评论

关于这篇文章

这篇文章由2hei2009年9月16日 18:05发布.

上一篇:BeautifulSoup has bug in rss tag 'link'

下一篇:ImportError: No module named xen.xm

回到首页 或者查看归档文章