【新人作品】抓取墨迹天气信息发送到微信(代码于2018/04/23更新)
本帖最后由 heywilliam 于 2018-4-23 10:18 编辑{:10_303:}
自学了Python四个月,自己写了个抓取墨迹天气三日天气预报 并且可以发送到指定好友或者微信群
还没有用函数进行封装,表示不太懂封装 欢迎各位帮忙优化代码谢谢!{:10_323:}
用到了beautifulsoup和正则表达式来清洗数据,算是一次比较好的实践了
import requests
from bs4 import BeautifulSoup
import re
from wxpy import *
url = 'https://tianqi.moji.com/weather/china/guangdong/tianhe-district' #这是广州天河区天气的链接,可以改为其他区
headers = {
'User-Agent': 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:46.0) Gecko/20100101 Firefox/46.0',
'Content-Type': 'application/x-www-form-urlencoded',
'Connection': 'Keep-Alive',
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8'
}
html = requests.get(url, headers=headers).text
soup = BeautifulSoup(html, "lxml")
weather_all = {}
# 当前天气\温度\更新时间\湿度\风向风力\温馨提示\空气质量
weather_all['current_weather'] = re.search(r'>([\u4e00-\u9fa5]{1,2})</', str(soup.select('.wea_weather b'))).group(1)
weather_all['current_temperature'] = re.search(r'>(\d\d?)</', str(soup.select('.wea_weather em'))).group(1)
weather_all['update_time'] = re.search(r'\u5929(\d\d?:\d\d?)\u66f4', str(soup.select('.wea_weather strong'))).group(1)
weather_all['humidity'] = re.search(r'n>(\u6e7f\u5ea6\s\d\d?%)</s', str(soup.select('.wea_about span'))).group(1)
weather_all['wind'] = re.search(r'm>([\u4e00-\u9fa5]+?\d+?\u7ea7)</', str(soup.select('.wea_about em'))).group(1)
weather_all['tips'] = re.search(r'm>([\u4e00-\u9fa5]+?[,]?[\u4e00-\u9fa5]+?[,]?[\u4e00-\u9fa5]+?。?)</', str(soup.select('.wea_tips em'))).group(1)
weather_all['aircon'] = re.search(r'em>(\d*\s[\u4e00-\u9fa5]+?)</e', str(soup.select('.wea_alert em'))).group(1)
# 提取天气预报内容
weathercast_list = soup.select('.forecast li')
# 今天天气\温度\风向\风力\空气质量
weather_all['today_weather'] = re.search(r'"(.+?)"', str(weathercast_list)).group(1)
weather_all['today_temperature'] = re.search(r'>(.+?)<', str(weathercast_list)).group(1)
weather_all['today_wind_direction'] = re.search(r'<em>(.+?)</em>', str(weathercast_list)).group(1)
weather_all['today_wind_force'] = re.search(r'<b>(.+?)</b>', str(weathercast_list)).group(1)
weather_all['today_aircon'] = re.search(r'">\s+(\d*\s[\u4e00-\u9fa5]+?)\s+?</st', str(weathercast_list)).group(1)
# 明天天气\温度\风向\风力\空气质量
weather_all['tmr_weather'] = re.search(r'"(.+?)"', str(weathercast_list)).group(1)
weather_all['tmr_temperature'] = re.search(r'>(.+?)<', str(weathercast_list)).group(1)
weather_all['tmr_wind_direction'] = re.search(r'<em>(.+?)</em>', str(weathercast_list)).group(1)
weather_all['tmr_wind_force'] = re.search(r'<b>(.+?)</b>', str(weathercast_list)).group(1)
weather_all['tmr_aircon'] = re.search(r'">\s+(\d*\s[\u4e00-\u9fa5]+?)\s+?</st', str(weathercast_list)).group(1)
# 后天天气\温度\风向\风力\空气质量
weather_all['datmr_weather'] = re.search(r'"(.+?)"', str(weathercast_list)).group(1)
weather_all['datmr_temperature'] = re.search(r'>(.+?)<', str(weathercast_list)).group(1)
weather_all['datmr_wind_direction'] = re.search(r'<em>(.+?)</em>', str(weathercast_list)).group(1)
weather_all['datmr_wind_force'] = re.search(r'<b>(.+?)</b>', str(weathercast_list)).group(1)
weather_all['datmr_aircon'] = re.search(r'">\s+(\d*\s[\u4e00-\u9fa5]+?)\s+?</st', str(weathercast_list)).group(1)
# 封装信息按格式输出
msg = []
msg.append('****广州天河区天气****')
msg.append(' ')
msg.append('数据于%s更新' % (weather_all['update_time']))
msg.append(' ')
msg.append('当前天气:%s' % (weather_all['current_weather']))
msg.append('当前气温:%s度' % (weather_all['current_temperature']))
msg.append('当前湿度:%s' % (weather_all['humidity']))
msg.append('当前风向\风力:%s' % (weather_all['wind']))
msg.append('当前空气质量:%s' % (weather_all['aircon']))
msg.append('温馨提示:%s' % (weather_all['tips']))
msg.append(' ')
msg.append('****三日天气预报****')
msg.append(' ')
msg.append('---今天---')
msg.append('天气:%s' % (weather_all['today_weather']))
msg.append('气温:%s度' % (weather_all['today_temperature']))
msg.append('风向\风力:%s%s' % (weather_all['today_wind_direction'], weather_all['today_wind_force']))
msg.append('空气质量:%s' % (weather_all['today_aircon']))
msg.append(' ')
msg.append('---明天---')
msg.append('天气:%s' % (weather_all['tmr_weather']))
msg.append('气温:%s度' % (weather_all['tmr_temperature']))
msg.append('风向\风力:%s%s' % (weather_all['tmr_wind_direction'], weather_all['tmr_wind_force']))
msg.append('空气质量:%s' % (weather_all['tmr_aircon']))
msg.append(' ')
msg.append('---后天---')
msg.append('天气:%s' % (weather_all['datmr_weather']))
msg.append('气温:%s度' % (weather_all['datmr_temperature']))
msg.append('风向\风力:%s%s' % (weather_all['datmr_wind_direction'], weather_all['datmr_wind_force']))
msg.append('空气质量:%s' % (weather_all['datmr_aircon']))
msg.append(' ')
msg.append('以上信息由Python机器人推送')
msg.append('数据来源:墨迹天气')
msg.append('Have a nice day :)')
msg = '\n'.join(msg) #插入换行符 实现换行输出
##以下为微信消息发送功能,如果需要启用请移除#号
##登陆微信
#bot = Bot()
#
## 获得群组名称
#group = bot.groups().search(u'******')# 此处输入群聊名称
#
## 获得好友名称
#friend = bot.friends().search(u'****') #此处输入好友名字
#
## 发送气象信息到群组
#group.send(msg)
#
## 发送气象信息给好友
#friend.send(msg)
print(msg)
最后呈现的输出:
****广州天河区天气****
数据于17:33更新
当前天气:多云
当前气温:24
当前湿度:湿度 49%
当前风向\风力:北风2级
当前空气质量:82 良
温馨提示:冷热适宜,感觉很舒适。
****三日天气预报****
---今天---
天气:多云
气温:14° / 25°
风向\风力:西北风1级
空气质量:82 良
---明天---
天气:多云
气温:18° / 25°
风向\风力:西北风1级
空气质量:53 良
---后天---
天气:雷阵雨
气温:20° / 25°
风向\风力:西北风1级
空气质量:44 优
以上信息由Python机器人推送
数据来源:墨迹天气
Have a nice day :)
{:10_275:} 彩蛋!
微信包wxpy的使用其实很简单而且很强大,具体使用说明请参考:http://wxpy.readthedocs.io/zh/latest/chats.html
祝大家使用愉快! 这个可以,我刚开始学 kankan 不错啊 来学习 学习一下{:10_277:} kan 看一看 {:5_90:} RE: 【新人作品】抓取墨迹天气信息发送到微信 [修改] 让人 学习学习 1 3 1
感谢楼主 的 辛勤劳作 我来看看啊 学习学习 学习一下
学习一下 666666666