当前位置: 首页 > OpenSource, python > 正文

python use BeautifulSoup crawl weather forecast

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 级

本文固定链接: http://2hei.net/python_use_beautifulsoup_crawl-weather-forecast.html | 2hei's site

该日志由 2hei 于2009年09月16日发表在 OpenSource, python 分类下,
原创文章转载请注明: python use BeautifulSoup crawl weather forecast | 2hei's site
关键字: ,

报歉!评论已关闭.