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.htmlimport 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 级
本文固定链接: https://www.2hei.net/2009/09/16/python_use_beautifulsoup_crawl-weather-forecast/ | 2hei.net
最活跃的读者