天气预报只能自己获取。
此脚本获取中国天气网七日预报,设备需要自建虚拟硬件,添加虚拟设备,设备类型选择Text文本。
效果: Python2.x代码:CNWeather.py
代码: 全选
#!/usr/bin/python
# -*- coding: UTF-8 -*-
# 中国天气数据
import urllib2
import re
#Domoticz服务器
domoticzserver = "127.0.0.1:8080"
#今日天气idx
today_idx = "49"
#明日天气idx
tomorrow_idx = "48"
#地区编号,来源:http://www.weather.com.cn/weather/101120501.shtml
area = "101120501"
#此方法向Domoticz服务器发送请求
def domoticzrequest (url):
request = urllib2.Request(url)
response = urllib2.urlopen(request)
return response.read()
'''
七日预报
'''
weather_url = "http://www.weather.com.cn/weather/"+area+".shtml"
weather_response = urllib2.urlopen(weather_url)
weather_result = weather_response.read()
# 七日预报结果
# 天气,weathers[0-6]
weathers = re.findall(u'title="(.*)" class="wea">',weather_result)
# 高低温度,temps[0-6][0-1]
#temps = re.findall(u'<span>(\d+)</span>/<i>(\d+)',weather_result)
temps = re.findall(u'<p class="tem">\n(?:<span>(-?\d+).*</span>/)?<i>(-?\d+)',weather_result)
#今日,weathers[0]
domoticzrequest("http://"+domoticzserver+"/json.htm?type=command¶m=udevice&idx="+today_idx+"&nvalue=0&svalue="+weathers[0]+","+(temps[0][0]+"/" if temps[0][0]!='' else "")+temps[0][1]+"℃")
#明日,weathers[1]
domoticzrequest("http://"+domoticzserver+"/json.htm?type=command¶m=udevice&idx="+tomorrow_idx+"&nvalue=0&svalue="+weathers[1]+","+temps[1][0]+"/"+temps[1][1]+"℃")
CNWeather3.py
代码: 全选
#!/usr/bin/python3
# -*- coding: UTF-8 -*-
# 中国天气数据
from urllib.parse import quote
import urllib.request
import re
#Domoticz服务器
domoticzserver = "127.0.0.1:2780"
#今日天气idx
today_idx = "29"
#明日天气idx
tomorrow_idx = "30"
#地区编号,来源:http://www.weather.com.cn/weather/101120501.shtml
area = "101120501"
#此方法向Domoticz服务器发送请求
def domoticzrequest (url):
response = urllib.request.urlopen(url)
return response.read()
#伪造来源Referer
header = {
'Host':'www.weather.com.cn',
'Referer':'http://www.weather.com.cn/',
'User-Agent':'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.181 Safari/537.36'
}
'''
七日预报
'''
weather_url = "http://www.weather.com.cn/weather/"+area+".shtml"
weather_request = urllib.request.Request(weather_url, None, header)
weather_response = urllib.request.urlopen(weather_request)
weather_result = weather_response.read().decode('utf-8')
# 七日预报结果
# 天气,weathers[0-6]
weathers = re.findall(u'title="(.*)" class="wea">',weather_result)
# 高低温度,temps[0-6][0-1]
#temps = re.findall(u'<span>(\d+)</span>/<i>(\d+)',weather_result)
temps = re.findall(u'<p class="tem">\n(?:<span>(-?\d+).*</span>/)?<i>(-?\d+)',weather_result)
#今日,weathers[0]
today_url = "http://"+domoticzserver+"/json.htm?type=command¶m=udevice&idx="+today_idx+"&nvalue=0&svalue="+weathers[0]+","+(temps[0][0]+"/" if temps[0][0]!='' else "")+temps[0][1]+"℃"
domoticzrequest(quote(today_url, safe='/:?=&'))
#明日,weathers[1]
tomorrow_url = "http://"+domoticzserver+"/json.htm?type=command¶m=udevice&idx="+tomorrow_idx+"&nvalue=0&svalue="+weathers[1]+","+temps[1][0]+"/"+temps[1][1]+"℃"
domoticzrequest(quote(tomorrow_url, safe='/:?=&'))
例如树莓派设置每30分钟执行一次:
代码: 全选
sudo crontab -e
代码: 全选
*/30 * * * * /home/pi/domoticz/scripts/python/CNWeather.py
Python2脚本 Python3脚本