100字范文,内容丰富有趣,生活中的好帮手!
100字范文 > php爬虫实时更新天气 Python爬虫获取中国天气网天气预报数据[-06-12更新]

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

时间:2022-12-13 16:32:35

相关推荐

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

实时天气显示建议用Domoticz内置的DarkSky。

天气预报只能自己获取。

此脚本获取中国天气网七日预报,设备需要自建虚拟硬件,添加虚拟设备,设备类型选择Text文本。

效果:

屏幕快照 -06-06 11.55.25.jpg (29.5 KiB) 查看 30878 次

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"

#地区编号,来源:/weather/101120501.shtml

area = "101120501"

#此方法向Domoticz服务器发送请求

def domoticzrequest (url):

request = urllib2.Request(url)

response = urllib2.urlopen(request)

return response.read()

'''

七日预报

'''

weather_url = "/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'(\d+)/(\d+)',weather_result)

temps = re.findall(u'

\n(?:(-?\d+).*/)?(-?\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"

#地区编号,来源:/weather/101120501.shtml

area = "101120501"

#此方法向Domoticz服务器发送请求

def domoticzrequest (url):

response = urllib.request.urlopen(url)

return response.read()

#伪造来源Referer

header = {

'Host':'',

'Referer':'/',

'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 = "/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'(\d+)/(\d+)',weather_result)

temps = re.findall(u'

\n(?:(-?\d+).*/)?(-?\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脚本

(2.2 KiB) 下载 552 次

Python3脚本

(1.77 KiB) 下载 603 次

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。