-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathget_weather.py
81 lines (66 loc) · 2.03 KB
/
get_weather.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#!/usr/bin/env python3
# coding: utf-8
import sys
import os
import re
import requests
from lxml import etree
def get_weather():
#定义保存结果的字典
dict = {'city_name' : u'南京'}
'''
city_name
day_temp
night_temp
day_weather
night_weather
'''
try:
header = {'User-Agent':'AppleWebKit/537.31 (KHTML, like Gecko) Chrome/26.0.1410.65 Safari/537.31 Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_2) '}
r = requests.get('http://www.nmc.gov.cn/publish/forecast/AJS/nan-jing.html', headers = header, timeout = 10)
r.encoding = 'utf-8'
html = r.text
tree = etree.HTML(html)
except Exception, e:
print e
sys.exit(1)
#HTML解析
rt = tree.xpath('//*[@id="forecast"]/div[1]/div[1]/table/tbody/tr[4]/td[1]')
if rt:
dict['day_temp'] = re.sub(r'\s', "", rt[0].text)
rt = tree.xpath('//*[@id="forecast"]/div[1]/div[1]/table/tbody/tr[4]/td[2]')
if rt:
dict['night_temp'] = re.sub(r'\s', "", rt[0].text)
rt = tree.xpath('//*[@id="forecast"]/div[1]/div[1]/table/tbody/tr[3]/td[1]')
if rt:
dict['day_weather'] = rt[0].text
rt = tree.xpath('//*[@id="forecast"]/div[1]/div[1]/table/tbody/tr[3]/td[2]')
if rt:
dict['night_weather'] = rt[0].text
rt = tree.xpath('//*[@id="forecast"]/div[1]/div[1]/table/tbody/tr[5]/td[1]')
if rt:
dict['wind_dir'] = rt[0].text
rt = tree.xpath('//*[@id="forecast"]/div[1]/div[1]/table/tbody/tr[6]/td[1]')
if rt:
dict['wind_class'] = rt[0].text
rt = tree.xpath('//*[@id="forecast"]/div[2]/div[2]/div[4]')
if rt:
dict['tomorrow_weather'] = re.sub(r'\s', "", rt[0].text)
rt = tree.xpath('//*[@id="forecast"]/div[2]/div[2]/div[5]')
if rt:
dict['tomorrow_temp'] = re.sub(r'\s', "", rt[0].text)
return dict
if __name__ == '__main__':
ret = get_weather()
print ret['city_name']
print ret['real_temp']
print ret['day_temp']
if ret.has_key('night_temp'):
print ret['night_temp']
print ret['day_weather']
if ret.has_key('night_weather'):
print ret['night_weather']
print ret['wind_dir']
print ret['wind_class']
print ret['tomorrow_weather']
print ret['tomorrow_temp']