Python 获取当地未来五天天气 天气预报 获取天气
说明
通过调用 天气网 接口实现获取当地天气的效果,已打包至函数方便调用,函数返回一个列表,列表组成如下,
[当地地名,发布时间,[[日期,[早天气现象,晚天气现象],[早气温,晚气温],[早风向,晚风向],[早风力,晚风力]]],…]
代码
# -*- coding:utf-8 -*-
"""
Python获取当地天气
BY我叫以赏 And Pikachu
转载注明出处
"""
import requests
def get_local_weather() -> list:
"""
此函数用户获取当地的天气,通过调用天气网接口:http://i.tianqi.com/index.php?c=code&id=57 实现效果。
BY 我叫以赏 And Pikachu
:return:(list)返回一个列表[当地地名,发布时间,[[日期,[早天气现象,晚天气现象],[早气温,晚气温],[早风向,晚风向],[早风力,晚风力]]],...]显示今天与未来五天
"""
# PositionText用于存储网页中关键文字出现的地方
PositionText = {
'area': ('<div class="top"><h1><a href="//www.tianqi.com/data/html/code_city.php">', '六天天气预报</a></h1><span>'),
'publishTime': ('</a></h1><span>(', '发布)</span>'),
'DayTime': ('<td width="15%" rowspan="2" bgcolor="#ffffff" class="f0">', '" target="_blank">', '</a></td>'),
'DayText':'<!--day',
'weather': ('<a href="http://wenzhou.tianqi.com/?tq" target="_blank">', '</a></td>')
}
data = requests.get('http://i.tianqi.com/index.php?c=code&id=57')#获取接口网页
weather_web = data.text # 天气原数据
# 获取地区
AreaTextPosition1 = weather_web.find(PositionText['area'][0]) + len(PositionText['area'][0]) # 寻找地区关键词前的字符
AreaTextPosition2 = weather_web.find(PositionText['area'][1], AreaTextPosition1) # 寻找地区关键词后面的字符
Area = weather_web[AreaTextPosition1:AreaTextPosition2] # 取出字符中间
# 获取发布日期 发布日期在AreaTextPosition2后面
publishTimePosition1 = weather_web.find(PositionText['publishTime'][0], AreaTextPosition2) + len(
PositionText['publishTime'][0]) # 寻找发布日期前的关键词
publishTimePosition2 = weather_web.find(PositionText['publishTime'][1], publishTimePosition1) # 寻找后面的
publishTime = weather_web[publishTimePosition1:publishTimePosition2] # 取出发布日期
# 取出天气
weatherPosition = publishTimePosition2 # 将光标移至发布日期后面,可以删除
weatherPosition = weather_web.find(PositionText['DayText'], weatherPosition) + len(PositionText['DayText']) # 寻找一天出现的标志
ret = [Area, publishTime] # 将前面获取的数据先存到列表内
while weatherPosition != len(PositionText['DayText']) - 1: # 不断循环直到找不到文本
DaytimePosition1 = weather_web.find(PositionText['DayTime'][0], weatherPosition) # 寻找时间出现位置
DaytimePosition2 = weather_web.find(PositionText['DayTime'][2], DaytimePosition1) # 寻找时间出现位置
DaytimePosition3 = weather_web.find(PositionText['DayTime'][1], DaytimePosition1)+len(PositionText['DayTime'][1]) # 由于时间位置特殊,没有专门的标志所以需要寻找三次
Daytime = weather_web[DaytimePosition3:DaytimePosition2].replace(' ', ' ').replace("<font color='green'>", '').replace("<font color='red'>", '').replace("</font>", '') # 替换文本中的额外数据
note = [] # 初始化获取天气数据
ZweatherPosition1 = DaytimePosition2 # 将贯标移到日期数据后面
for x in range(10): # 获取天气数据
ZweatherPosition1 = weather_web.find(PositionText['weather'][0],ZweatherPosition1)+len(PositionText['weather'][0]) # 寻找天气开始位置
if weather_web[ZweatherPosition1:ZweatherPosition1+4]=='<img': # 过滤无效数据
continue
ZweatherPosition2 = weather_web.find(PositionText['weather'][1], ZweatherPosition1) # 寻找天气结束位置
note.append(weather_web[ZweatherPosition1:ZweatherPosition2])#添加到末尾
ret.append([[ Daytime, [note[0],note[4]],[note[1], note[5]],[note[2], note[6]],[note[3], note[7]] ]]) # 添加数据到返回列表末尾
weatherPosition = weather_web.find(PositionText['DayText'], weatherPosition) + len(PositionText['DayText'])#寻找下一天
return ret#返回
if __name__ == '__main__':
print('此程序会获取当地的天气,BY 我叫以赏 And Pikachu !\n'
'转载请注明出处!源码已打包成函数 get_local_weather() 使用了 requests 模块。\n'
'请稍等片刻正在调用 天气网 的数据......')
try:
weather = get_local_weather()
except BaseException as error:
print('Error:获取错误!原因:'+str(error))
else:
print('获取城市:'+weather[0])
print('发布天气时间:'+weather[1])
print('-' * 55)
for x in range(2,7):
print('=日期:' + weather[x][0][0]+'=')
print('>白天<')
print('天气现象:%s 气温:%s 风向:%s 风速:%s'%
(weather[x][0][1][0],weather[x][0][2][0],weather[x][0][3][0],weather[x][0][4][0]))
print('>晚上<')
print('天气现象:%s 气温:%s 风向:%s 风速:%s' %
(weather[x][0][1][1], weather[x][0][2][1], weather[x][0][3][1], weather[x][0][4][1]))
print('-' * 55)
input('回车退出[Enter]')
下载地址:https://ysdmmxw.coding.net/api/share/downl...
本作品采用《CC 协议》,转载必须注明作者和本文链接
好棒