美文网首页大数据 爬虫Python AI SqlPython小哥哥
Python获取地震信息!能预测地震!

Python获取地震信息!能预测地震!

作者: 14e61d025165 | 来源:发表于2019-06-20 15:11 被阅读1次

6月17日22分25分,四川省宜宾市长宁县发生了6.0级地震,成都高新减灾研究所与应急管理部门联合建设的大陆地震预警网成功预警本次地震,提前10秒向宜宾市预警,提前61秒向成都预警。

<tt-image data-tteditor-tag="tteditorTag" contenteditable="false" class="syl1561014625698" data-render-status="finished" data-syl-blot="image" style="box-sizing: border-box; cursor: text; color: rgb(34, 34, 34); font-family: "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "WenQuanYi Micro Hei", "Helvetica Neue", Arial, sans-serif; font-size: 16px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; white-space: pre-wrap; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial; display: block;"> image

<input class="pgc-img-caption-ipt" placeholder="图片描述(最多50字)" value="" style="box-sizing: border-box; outline: 0px; color: rgb(102, 102, 102); position: absolute; left: 187.5px; transform: translateX(-50%); padding: 6px 7px; max-width: 100%; width: 375px; text-align: center; cursor: text; font-size: 12px; line-height: 1.5; background-color: rgb(255, 255, 255); background-image: none; border: 0px solid rgb(217, 217, 217); border-radius: 4px; transition: all 0.2s cubic-bezier(0.645, 0.045, 0.355, 1) 0s;"></tt-image>

虽然自己还不能写出这么牛逼的系统,但是今天我想结合自己学到的Python知识,用Python获取地震信息,然后微信实时推送给你的群组或你的朋友。

1.前期准备

1.爬虫基本知识,比如requests库,以及lxml库;

2.利用Xpath进行HTML的解析;

之前写的一些简单项目,提取页面信息时使用的是正则表达式,但当项目复杂时,用正则表达式比较烦琐,万一有地方写错了,可能导致匹配失败,所以使用正则表达式提取页面信息多多少少还是有些不方便。

通过最近学习,我知道了在网页中可以通过Xpath或CSS选择器来定位一个或多个节点,再调用相应的方法获取它的正文内容或者属性,可以很方便快捷的提取到我们想要的信息。

3.要实现微信实时推送肯定需要用到wxpy库;

4.本次项目从中国地震台网爬取地震信息,链接为: http:// news.ceic.ac.cn/index.h tml?time= {int(time.time())}。

<tt-image data-tteditor-tag="tteditorTag" contenteditable="false" class="syl1561014625702" data-render-status="finished" data-syl-blot="image" style="box-sizing: border-box; cursor: text; color: rgb(34, 34, 34); font-family: "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "WenQuanYi Micro Hei", "Helvetica Neue", Arial, sans-serif; font-size: 16px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; white-space: pre-wrap; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial; display: block;"> image

<input class="pgc-img-caption-ipt" placeholder="图片描述(最多50字)" value="" style="box-sizing: border-box; outline: 0px; color: rgb(102, 102, 102); position: absolute; left: 187.5px; transform: translateX(-50%); padding: 6px 7px; max-width: 100%; width: 375px; text-align: center; cursor: text; font-size: 12px; line-height: 1.5; background-color: rgb(255, 255, 255); background-image: none; border: 0px solid rgb(217, 217, 217); border-radius: 4px; transition: all 0.2s cubic-bezier(0.645, 0.045, 0.355, 1) 0s;"></tt-image>

2.代码整合

<pre spellcheck="false" style="box-sizing: border-box; margin: 5px 0px; padding: 5px 10px; border: 0px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-variant-numeric: inherit; font-variant-east-asian: inherit; font-weight: 400; font-stretch: inherit; font-size: 16px; line-height: inherit; font-family: inherit; vertical-align: baseline; cursor: text; counter-reset: list-1 0 list-2 0 list-3 0 list-4 0 list-5 0 list-6 0 list-7 0 list-8 0 list-9 0; background-color: rgb(240, 240, 240); border-radius: 3px; white-space: pre-wrap; color: rgb(34, 34, 34); letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">import requests, time Python学习交流群:1004391443
from lxml import etree
from wxpy import *

微信登陆

bot = Bot()

查找好友

my_friend = bot.friends().search(u'stormwen')[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分钟更新一次)'
# 发送信息
my_friend.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分钟更新一次)'
# 发送信息
my_friend.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)
</pre>

3.结果展示

<tt-image data-tteditor-tag="tteditorTag" contenteditable="false" class="syl1561014625713" data-render-status="finished" data-syl-blot="image" style="box-sizing: border-box; cursor: text; color: rgb(34, 34, 34); font-family: "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "WenQuanYi Micro Hei", "Helvetica Neue", Arial, sans-serif; font-size: 16px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; white-space: pre-wrap; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial; display: block;"> image

<input class="pgc-img-caption-ipt" placeholder="图片描述(最多50字)" value="" style="box-sizing: border-box; outline: 0px; color: rgb(102, 102, 102); position: absolute; left: 187.5px; transform: translateX(-50%); padding: 6px 7px; max-width: 100%; width: 375px; text-align: center; cursor: text; font-size: 12px; line-height: 1.5; background-color: rgb(255, 255, 255); background-image: none; border: 0px solid rgb(217, 217, 217); border-radius: 4px; transition: all 0.2s cubic-bezier(0.645, 0.045, 0.355, 1) 0s;"></tt-image>

4.总结

我一直认为语言只是工具,只有用它来做点具体的事,才体现出它的价值。今天这个项目用到了Python的爬虫知识,没有用大家讨厌的正则表达式,而是用一种新的方式解析库,实现对HTML的解析和提取信息,最后又用到前面项目用过的wxpy库,实现了全部功能。

相关文章

  • Python获取地震信息!能预测地震!

    6月17日22分25分,四川省宜宾市长宁县发生了6.0级地震,成都高新减灾研究所与应急管理部门联合建设的大陆地震预...

  • 使用python获取地震信息

    6月17日22分25分,四川省宜宾市长宁县发生了6.0级地震,成都高新减灾研究所与应急管理部门联合建设的大陆地震预...

  • 地震如何预测?

    地震预测,一直是世界性难题。 知道地震存在,却不知何时到来。 如今地震不是预测,最多算预报。 地震有纵波和横波。 ...

  • 地震为何无法被预测

    地震是一种自然现象,其原因是非常复杂的,目前也没有人能预测地震。至少现在的理论和水平做不到。 地震...

  • 为何地震无法预测?

    在观看了《地震为何无法预测?》后,我来说说我对为何无法预测地震的看法。在记录片上说到为了预测下一个地震的来临,外国...

  • 地震为何无法预测

    在观看了《地震为何无法预测?》后,我来说说我对为何无法预测地震的看法。 在记录片上说到为了预测下一个地震的来临,外...

  • 地震为何无法预测

    在我观看了《地震为何无法预测?》后,我来说说我对为何无法预测地震的看法。在记录片上说到为了预测下一个地震的来临,外...

  • 【我与地震】地震可以提前预测吗

    百度说地震是不可以提前预测的。 当前的科技水平尚无法预测地震的到来,未来相当长的一段时间内,地震也是无法预测的。所...

  • 《信号与噪声》第五章 地震预测 过度拟合与预测的边界

    △地震的预测和不可预测 美国地质调查局的官方立场是:地震时不可预测的。 如果引入预言的概念,和预测并列: 预言:对...

  • 地震无法预测?!

    地震是人们非常抵触的一个词语,答案显而易见,可能会令很多人受伤甚至失去生命。那么如果地震可以预测,结果应该是很...

网友评论

    本文标题:Python获取地震信息!能预测地震!

    本文链接:https://www.haomeiwen.com/subject/lefjqctx.html