Python爬虫获取中国天气网天气预报数据[2018-06-12更新]

回复
头像
DT27
帖子: 345
注册时间: 周四 3月 30, 2017 08:54
Gender:

Python爬虫获取中国天气网天气预报数据[2018-06-12更新]

帖子 DT27 »

实时天气显示建议用Domoticz内置的DarkSky。
天气预报只能自己获取。
此脚本获取中国天气网七日预报,设备需要自建虚拟硬件,添加虚拟设备,设备类型选择Text文本。
效果:
屏幕快照 2017-06-06 11.55.25.jpg
屏幕快照 2017-06-06 11.55.25.jpg (29.5 KiB) 查看 66874 次
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&param=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&param=udevice&idx="+tomorrow_idx+"&nvalue=0&svalue="+weathers[1]+","+temps[1][0]+"/"+temps[1][1]+"℃")

Python3版:
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&param=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&param=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脚本
CNWeather.py
(2.2 KiB) 已下载 1494 次
Python3脚本
CNWeather3.py
(1.77 KiB) 已下载 1584 次
附件
CNWeather3.py
(1.46 KiB) 已下载 1463 次
chinesesich
帖子: 26
注册时间: 周二 6月 06, 2017 13:50

Re: py爬虫获取中国天气网天气预报数据

帖子 chinesesich »

楼主,极速数据有天气预报的api,能否整合到Domoticz里面?
头像
DT27
帖子: 345
注册时间: 周四 3月 30, 2017 08:54
Gender:

Re: py爬虫获取中国天气网天气预报数据

帖子 DT27 »

chinesesich 写了: 周六 6月 10, 2017 17:38 楼主,极速数据有天气预报的api,能否整合到Domoticz里面?
等有时间看看,另外目前就算做也是一样的脚本形式,不会做成插件集成进domoticz

刚看了,收费的。。。还是爬网页吧。
头像
DT27
帖子: 345
注册时间: 周四 3月 30, 2017 08:54
Gender:

Re: py爬虫获取中国天气网天气预报数据

帖子 DT27 »

刚更新了脚本,修复晚上仅有低温数据导致的bug。
blindlight
帖子: 98
注册时间: 周四 3月 30, 2017 00:03

Re: Python爬虫获取中国天气网天气预报数据[2017-06-30更新]

帖子 blindlight »

求python3版本
头像
DT27
帖子: 345
注册时间: 周四 3月 30, 2017 08:54
Gender:

Re: Python爬虫获取中国天气网天气预报数据[2017-06-30更新]

帖子 DT27 »

blindlight 写了: 周二 7月 04, 2017 11:34 求python3版本
python3版发了 :D
chinesesich
帖子: 26
注册时间: 周二 6月 06, 2017 13:50

Re: py爬虫获取中国天气网天气预报数据

帖子 chinesesich »

DT27 写了: 周六 6月 10, 2017 21:06
chinesesich 写了: 周六 6月 10, 2017 17:38 楼主,极速数据有天气预报的api,能否整合到Domoticz里面?
等有时间看看,另外目前就算做也是一样的脚本形式,不会做成插件集成进domoticz

刚看了,收费的。。。还是爬网页吧。
送券。买完了能用21000次
chinesesich
帖子: 26
注册时间: 周二 6月 06, 2017 13:50

Re: Python爬虫获取中国天气网天气预报数据[2017-06-30更新]

帖子 chinesesich »

pi@raspberrypi:~/domoticz/scripts/python $ sudo python3 CNweather3.py
Traceback (most recent call last):
File "CNweather3.py", line 39, in <module>
today_url = "http://"+domoticzserver+"/json.htm?type=command&param=udevice&idx="+today_idx+"&nvalue=0&svalue="+weathers[0]+","+(temps[0][0]+"/" if temps[0][0]!='' else "")+temps[0][1]+"℃"
IndexError: list index out of range

楼主,报这个错误
头像
DT27
帖子: 345
注册时间: 周四 3月 30, 2017 08:54
Gender:

Re: Python爬虫获取中国天气网天气预报数据[2017-06-30更新]

帖子 DT27 »

chinesesich 写了: 周三 7月 05, 2017 20:20 pi@raspberrypi:~/domoticz/scripts/python $ sudo python3 CNweather3.py
Traceback (most recent call last):
File "CNweather3.py", line 39, in <module>
today_url = "http://"+domoticzserver+"/json.htm?type=command&param=udevice&idx="+today_idx+"&nvalue=0&svalue="+weathers[0]+","+(temps[0][0]+"/" if temps[0][0]!='' else "")+temps[0][1]+"℃"
IndexError: list index out of range

楼主,报这个错误
Sorry,之前没有处理零下温度,脚本已更新。
HelloCici
帖子: 2
注册时间: 周二 2月 27, 2018 16:49

Re: Python爬虫获取中国天气网天气预报数据[2017-12-11更新]

帖子 HelloCici »

Windows 的怎么用
头像
DT27
帖子: 345
注册时间: 周四 3月 30, 2017 08:54
Gender:

Re: Python爬虫获取中国天气网天气预报数据[2017-12-11更新]

帖子 DT27 »

HelloCici 写了: 周二 2月 27, 2018 17:41 Windows 的怎么用
Windows也有计划任务啊,直接把CNWeather.py文件加进计划任务里定时执行。
头像
DT27
帖子: 345
注册时间: 周四 3月 30, 2017 08:54
Gender:

Re: Python爬虫获取中国天气网天气预报数据[2018-06-12更新]

帖子 DT27 »

Python3脚本更新,防屏蔽。
pizixiaotian
帖子: 29
注册时间: 周三 4月 05, 2017 22:15

Re: Python爬虫获取中国天气网天气预报数据[2018-06-12更新]

帖子 pizixiaotian »

2018.12.1号以后获取不到数据了?
头像
Admin
网站管理员
帖子: 118
注册时间: 周六 2月 25, 2017 12:47
Gender:

Re: Python爬虫获取中国天气网天气预报数据[2018-06-12更新]

帖子 Admin »

pizixiaotian 写了: 周一 1月 07, 2019 17:33 2018.12.1号以后获取不到数据了?
目前一切正常。
屏幕快照 2019-01-08 10.15.03.jpg
屏幕快照 2019-01-08 10.15.03.jpg (19.03 KiB) 查看 61654 次
codeprince
帖子: 4
注册时间: 周六 9月 05, 2020 03:53

今日预报晚上只有最低温度?

帖子 codeprince »

您好!今日预报晚上只有最低温度,没有最高温度。
使用环境是python3.7
微信图片_20201030215623.png
微信图片_20201030215623.png (48.64 KiB) 查看 38223 次
头像
DT27
帖子: 345
注册时间: 周四 3月 30, 2017 08:54
Gender:

Re: 今日预报晚上只有最低温度?

帖子 DT27 »

codeprince 写了: 周五 10月 30, 2020 21:58 您好!今日预报晚上只有最低温度,没有最高温度。
使用环境是python3.7微信图片_20201030215623.png
预报哈,最高气温都是白天,晚上就不显示了。这是中国天气自己的逻辑,我想了想也没毛病,就没改 :lol:
codeprince
帖子: 4
注册时间: 周六 9月 05, 2020 03:53

Re: Python爬虫获取中国天气网天气预报数据[2018-06-12更新]

帖子 codeprince »

好的 经试验,白天是能正常显示最高温、最低温的, 谢谢!
回复