码迷,mamicode.com
首页 > 编程语言 > 详细

python抓取天气

时间:2015-01-30 22:50:00      阅读:202      评论:0      收藏:0      [点我收藏+]

标签:python   数据   

程序很简单

根据url找到网站截取相应的数据。

#!/usr/bin/env python

import urllib2
import re

provice=raw_input('input you provice')
city=raw_input("input city:")
url = "http://qq.ip138.com/weather/"+provice+"/"+city+".htm"
#url = "http://qq.ip138.com/weather/anhui/HuoShan.htm"

class Weather():
        def __init__(self):
                pass

        def getHtml(self, url):
                req = urllib2.Request(url)
                res = urllib2.urlopen(req)
                html = res.read()
                res.close()
                return html

        def getWeather(self, html):
                patterndate = re.compile(">(\d{4}-\d{1,2}-\d{1,2} .+)<")
                date = patterndate.findall(html)

                patternweather = re.compile("<br/>(.+)</td>")
                weather = patternweather.findall(html)

                patterntemperature = re.compile("<td>([-]?\d{1,2}.+)</td>")
                temperature = patterntemperature.findall(html)

                if len(date) == 0:
                        print "Cannot get city's weather you input"
                        return
                for i in range(len(date)):
                        print "%s" % date[i], "\t%s" % weather[i], "\t%s" % temperature[i]

if __name__ == "__main__":
        weather = Weather()
        weather.getWeather(weather.getHtml(url))

后期准备存库

 

 

python抓取天气

标签:python   数据   

原文地址:http://blog.csdn.net/u010278923/article/details/43309923

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!