鱼C论坛

 找回密码
 立即注册
查看: 13274|回复: 318

[作品展示] 【新人作品】抓取墨迹天气信息发送到微信(代码于2018/04/23更新)

  [复制链接]
发表于 2018-4-17 18:02:53 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
本帖最后由 heywilliam 于 2018-4-23 10:18 编辑


自学了Python四个月,自己写了个抓取墨迹天气三日天气预报 并且可以发送到指定好友或者微信群
还没有用函数进行封装,表示不太懂封装 欢迎各位帮忙优化代码  谢谢!
用到了beautifulsoup和正则表达式来清洗数据,算是一次比较好的实践了


  1. import requests
  2. from bs4 import BeautifulSoup
  3. import re
  4. from wxpy import *

  5. url = 'https://tianqi.moji.com/weather/china/guangdong/tianhe-district' #这是广州天河区天气的链接,可以改为其他区

  6. headers = {
  7.     'User-Agent': 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:46.0) Gecko/20100101 Firefox/46.0',
  8.     'Content-Type': 'application/x-www-form-urlencoded',
  9.     'Connection': 'Keep-Alive',
  10.     'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8'
  11. }

  12. html = requests.get(url, headers=headers).text

  13. soup = BeautifulSoup(html, "lxml")

  14. weather_all = {}

  15. # 当前天气\温度\更新时间\湿度\风向风力\温馨提示\空气质量
  16. weather_all['current_weather'] = re.search(r'>([\u4e00-\u9fa5]{1,2})</', str(soup.select('.wea_weather b'))).group(1)
  17. weather_all['current_temperature'] = re.search(r'>(\d\d?)</', str(soup.select('.wea_weather em'))).group(1)
  18. weather_all['update_time'] = re.search(r'\u5929(\d\d?:\d\d?)\u66f4', str(soup.select('.wea_weather strong'))).group(1)
  19. weather_all['humidity'] = re.search(r'n>(\u6e7f\u5ea6\s\d\d?%)</s', str(soup.select('.wea_about span'))).group(1)
  20. weather_all['wind'] = re.search(r'm>([\u4e00-\u9fa5]+?\d+?\u7ea7)</', str(soup.select('.wea_about em'))).group(1)
  21. weather_all['tips'] = re.search(r'm>([\u4e00-\u9fa5]+?[,]?[\u4e00-\u9fa5]+?[,]?[\u4e00-\u9fa5]+?。?)</', str(soup.select('.wea_tips em'))).group(1)
  22. weather_all['aircon'] = re.search(r'em>(\d*\s[\u4e00-\u9fa5]+?)</e', str(soup.select('.wea_alert em'))).group(1)

  23. # 提取天气预报内容
  24. weathercast_list = soup.select('.forecast li')

  25. # 今天天气\温度\风向\风力\空气质量
  26. weather_all['today_weather'] = re.search(r'"(.+?)"', str(weathercast_list[4])).group(1)
  27. weather_all['today_temperature'] = re.search(r'>(.+?)<', str(weathercast_list[5])).group(1)
  28. weather_all['today_wind_direction'] = re.search(r'<em>(.+?)</em>', str(weathercast_list[6])).group(1)
  29. weather_all['today_wind_force'] = re.search(r'<b>(.+?)</b>', str(weathercast_list[6])).group(1)
  30. weather_all['today_aircon'] = re.search(r'">\s+(\d*\s[\u4e00-\u9fa5]+?)\s+?</st', str(weathercast_list[7])).group(1)

  31. # 明天天气\温度\风向\风力\空气质量
  32. weather_all['tmr_weather'] = re.search(r'"(.+?)"', str(weathercast_list[9])).group(1)
  33. weather_all['tmr_temperature'] = re.search(r'>(.+?)<', str(weathercast_list[10])).group(1)
  34. weather_all['tmr_wind_direction'] = re.search(r'<em>(.+?)</em>', str(weathercast_list[11])).group(1)
  35. weather_all['tmr_wind_force'] = re.search(r'<b>(.+?)</b>', str(weathercast_list[11])).group(1)
  36. weather_all['tmr_aircon'] = re.search(r'">\s+(\d*\s[\u4e00-\u9fa5]+?)\s+?</st', str(weathercast_list[12])).group(1)

  37. # 后天天气\温度\风向\风力\空气质量
  38. weather_all['datmr_weather'] = re.search(r'"(.+?)"', str(weathercast_list[14])).group(1)
  39. weather_all['datmr_temperature'] = re.search(r'>(.+?)<', str(weathercast_list[15])).group(1)
  40. weather_all['datmr_wind_direction'] = re.search(r'<em>(.+?)</em>', str(weathercast_list[16])).group(1)
  41. weather_all['datmr_wind_force'] = re.search(r'<b>(.+?)</b>', str(weathercast_list[16])).group(1)
  42. weather_all['datmr_aircon'] = re.search(r'">\s+(\d*\s[\u4e00-\u9fa5]+?)\s+?</st', str(weathercast_list[17])).group(1)

  43. # 封装信息按格式输出
  44. msg = []
  45. msg.append('****广州天河区天气****')
  46. msg.append(' ')
  47. msg.append('数据于%s更新' % (weather_all['update_time']))
  48. msg.append(' ')
  49. msg.append('当前天气:%s' % (weather_all['current_weather']))
  50. msg.append('当前气温:%s度' % (weather_all['current_temperature']))
  51. msg.append('当前湿度:%s' % (weather_all['humidity']))
  52. msg.append('当前风向\风力:%s' % (weather_all['wind']))
  53. msg.append('当前空气质量:%s' % (weather_all['aircon']))
  54. msg.append('温馨提示:%s' % (weather_all['tips']))
  55. msg.append(' ')
  56. msg.append('****三日天气预报****')
  57. msg.append(' ')
  58. msg.append('---今天---')
  59. msg.append('天气:%s' % (weather_all['today_weather']))
  60. msg.append('气温:%s度' % (weather_all['today_temperature']))
  61. msg.append('风向\风力:%s%s' % (weather_all['today_wind_direction'], weather_all['today_wind_force']))
  62. msg.append('空气质量:%s' % (weather_all['today_aircon']))
  63. msg.append(' ')
  64. msg.append('---明天---')
  65. msg.append('天气:%s' % (weather_all['tmr_weather']))
  66. msg.append('气温:%s度' % (weather_all['tmr_temperature']))
  67. msg.append('风向\风力:%s%s' % (weather_all['tmr_wind_direction'], weather_all['tmr_wind_force']))
  68. msg.append('空气质量:%s' % (weather_all['tmr_aircon']))
  69. msg.append(' ')
  70. msg.append('---后天---')
  71. msg.append('天气:%s' % (weather_all['datmr_weather']))
  72. msg.append('气温:%s度' % (weather_all['datmr_temperature']))
  73. msg.append('风向\风力:%s%s' % (weather_all['datmr_wind_direction'], weather_all['datmr_wind_force']))
  74. msg.append('空气质量:%s' % (weather_all['datmr_aircon']))
  75. msg.append(' ')
  76. msg.append('以上信息由Python机器人推送')
  77. msg.append('数据来源:墨迹天气')
  78. msg.append('Have a nice day :)')

  79. msg = '\n'.join(msg) #插入换行符 实现换行输出

  80. ##以下为微信消息发送功能,如果需要启用请移除#号
  81. ##登陆微信
  82. #bot = Bot()
  83. #
  84. ## 获得群组名称
  85. #group = bot.groups().search(u'******')[0]  # 此处输入群聊名称
  86. #
  87. ## 获得好友名称
  88. #friend = bot.friends().search(u'****')[0]   #此处输入好友名字
  89. #
  90. ## 发送气象信息到群组
  91. #group.send(msg)
  92. #
  93. ## 发送气象信息给好友
  94. #friend.send(msg)

  95. print(msg)
复制代码


最后呈现的输出:

  1. ****广州天河区天气****

  2. 数据于17:33更新

  3. 当前天气:多云
  4. 当前气温:24
  5. 当前湿度:湿度 49%
  6. 当前风向\风力:北风2级
  7. 当前空气质量:82 良
  8. 温馨提示:冷热适宜,感觉很舒适。

  9. ****三日天气预报****

  10. ---今天---
  11. 天气:多云
  12. 气温:14° / 25°
  13. 风向\风力:西北风1级
  14. 空气质量:82 良

  15. ---明天---
  16. 天气:多云
  17. 气温:18° / 25°
  18. 风向\风力:西北风1级
  19. 空气质量:53 良

  20. ---后天---
  21. 天气:雷阵雨
  22. 气温:20° / 25°
  23. 风向\风力:西北风1级
  24. 空气质量:44 优

  25. 以上信息由Python机器人推送
  26. 数据来源:墨迹天气
  27. Have a nice day :)
复制代码


彩蛋!

微信包wxpy的使用其实很简单而且很强大,具体使用说明请参考:http://wxpy.readthedocs.io/zh/latest/chats.html

祝大家使用愉快!
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2018-4-17 18:18:51 From FishC Mobile | 显示全部楼层
这个可以,我刚开始学
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-4-17 18:27:21 | 显示全部楼层
kankan
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 1 反对 0

使用道具 举报

发表于 2018-4-17 18:29:29 | 显示全部楼层
不错啊 来学习
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-4-17 18:55:59 | 显示全部楼层
学习一下
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-4-17 21:31:15 | 显示全部楼层
kan
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2018-4-19 12:47:36 | 显示全部楼层
看一看
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-4-19 12:53:24 | 显示全部楼层
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2018-4-19 13:29:59 | 显示全部楼层
RE: 【新人作品】抓取墨迹天气信息发送到微信 [修改]
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-4-19 15:16:15 | 显示全部楼层
让人
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2018-4-19 15:19:26 | 显示全部楼层
学习学习
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-4-19 16:27:54 | 显示全部楼层
1
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2018-4-19 22:08:12 | 显示全部楼层
3
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2018-4-19 22:13:54 | 显示全部楼层
1
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2018-4-19 23:43:05 | 显示全部楼层
感谢楼主 的 辛勤劳作
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-4-19 23:50:23 | 显示全部楼层
我来看看啊
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-4-20 06:45:33 | 显示全部楼层
学习学习
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-4-20 10:08:57 | 显示全部楼层
学习一下
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-4-20 11:34:08 | 显示全部楼层
学习一下
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-4-20 11:42:48 | 显示全部楼层
666666666
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-4-18 09:50

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表