脚本之家,脚本语言编程技术及教程分享平台!
分类导航

Python|VBS|Ruby|Lua|perl|VBA|Golang|PowerShell|Erlang|autoit|Dos|bat|

服务器之家 - 脚本之家 - Python - python获取地震信息 微信实时推送

python获取地震信息 微信实时推送

2021-07-15 09:371874445373 Python

这篇文章主要为大家详细介绍了python获取地震信息,微信实时推送,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

本文实例为大家分享了python获取地震信息微信实时推送的具体代码,供大家参考,具体内容如下

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import requests,time
from lxml import etree
from wxpy import *
 
# 微信登陆
bot = bot()
# 查找好友
group = bot.groups().search('珍爱生命 远离lisp')[0] #写自己的讨论组名称
 
with open('log.txt', 'r') as f:
 rember = f.readline()
 
headers = {
  'user-agent': 'mozilla/5.0 (windows nt 10.0; win64; x64) applewebkit/537.36 (khtml, like gecko) chrome/70.0.3538.110 safari/537.36',
  'cookie':'hm_lvt_e0025cd5d352165f8a646ccea5beb27d=1543211803; hm_lpvt_e0025cd5d352165f8a646ccea5beb27d=1543211803',
 }
 
while true:
 try:
  url = f'http://news.ceic.ac.cn/index.html?time={int(time.time())}'
  # 请求数据
  res = requests.get(url,headers = headers).text.encode('iso-8859-1').decode('utf8')
  html_ele = etree.html(res)
  #返回列表
  res = html_ele.xpath('//*[@id="news"]//td//text()')
 
  # 如果日志为空,发送最新的一条地震信息
  if rember == '':
   msg = f'北京时间:{res[1]},在纬度:{res[2]} ,经度{res[3]} 处发生了{res[0]}级地震,震源深度{res[4]}千米,参考位置:{res[5]}(5分钟更新一次)'
   # 发送信息
   group.send(msg)
   print('日志为空,msg:', msg)
 
  # 如果日志非空,就判断是否是最新的,发送日志之后的所有新的数据
  else:
   i = res.index(rember)
   while i > 1:
    i -= 6
    msg = f'北京时间:{res[i]},在纬度:{res[i+1]} ,经度{res[i+2]} 处发生了{res[i-1]}级地震,震源深度{res[i+3]}千米,参考位置:{res[i+4]}(5分钟更新一次)'
    # 发送信息
    group.send(msg)
    print('日志非空,msg:',msg)
 
  time.sleep(300)
  rember = res[1]
  # 更新日志(记录最新发送的地震信息)
  with open('log.txt', 'w') as f:
   f.write(res[1])
 except:
  time.sleep(60)

# 运行后10秒左右会有弹框显示二维码,扫描后即登陆自己的账号,并选择匹配的讨论组名称进行发送.

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。

原文链接:https://blog.csdn.net/qq_35515661/article/details/84557042

延伸 · 阅读

精彩推荐