-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgetWeather.py
61 lines (52 loc) · 2.13 KB
/
getWeather.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#!/usr/binenv python
# -*- coding: UTF-8 -*-
import json
import urllib2
from BeautifulSoup import BeautifulStoneSoup
import getIpAddr
def getJsonUrlNo(province, city):
'''解析xml信息,提取city的json数据对于的url号,返回一个uncode字
符串'''
baseUrl = 'http://flash.weather.com.cn/wmaps/xml/'
url = baseUrl + province + '.xml'
xml = urllib2.urlopen(url).read()
soup = BeautifulStoneSoup(xml)
cityList = soup.findAll('city',{'pyname':city})
cityUrl = cityList[0]['url']
return cityUrl
def getJsonInfo(province, city):
'''根据province,city参数查找city的json格式的天气信息,返回一个dict'''
baseUrl = 'http://m.weather.com.cn/data/'
jsonUrlNo = getJsonUrlNo(province, city)
jsonUrl = baseUrl + jsonUrlNo + '.html'
weatherHtml = urllib2.urlopen(jsonUrl).read()
weatherJSON = json.JSONDecoder().decode(weatherHtml)
weatherInfo = weatherJSON['weatherinfo']
return weatherInfo
def printDayWeather(weatherInfo):
'''格式化输出今明后三天的天气信息'''
print '城市:', weatherInfo['city']
print '日期:', weatherInfo['date_y']
print '1.今天天气:'
print ' 温度:', weatherInfo['temp1']
print ' 天气:', weatherInfo['weather1']
print ' 风速:', weatherInfo['wind1']
print ' 紫外线:', weatherInfo['index_uv']
print ' 穿衣指数:', weatherInfo['index_d']
print '2.明天天气:'
print ' 温度:', weatherInfo['temp2']
print ' 天气:', weatherInfo['weather2']
print ' 风速:', weatherInfo['wind2']
print ' 紫外线:', weatherInfo['index48_uv']
print ' 穿衣指数:', weatherInfo['index48_d']
print '3.后天天气:'
print ' 温度:', weatherInfo['temp3']
print ' 天气:', weatherInfo['weather3']
print ' 风速:', weatherInfo['wind3']
if __name__ == '__main__':
# 获取基于ip的地理位置
location = getIpAddr.getIpAddr()
city, province = location[0].lower(), location[1].lower()
dayInfo = getJsonInfo(province, city)
# 输出全天天气
printDayWeather(dayInfo)