繁体   English   中英

如何抓取生成更新数据表的网页

[英]How to scrape webpage that produce updated data table

我正在尝试使用漂亮的汤为网站 Scraper 构建一个简单的 Python 代码。

该网站是一个地震监测站点,它看起来很复杂并且与地理地图集成在一起,但我只对其中一个表格信息感兴趣,该信息只会在地震发生(或进行预测)时更新,我需要的信息是在右下角的“更多”按钮下,您可以选择一个特定的感兴趣的城市。

我想要做的是,抓取这些信息,检查最新信息是否有任何更新,如果“目标首府/城市中的最大地震强度”列更新为大于 4 的数字(最新数据)顶行),我希望代码能够返回真/假布尔输出。(因此我可以使用 LabView 中的代码模块控制仪器)

任何人都可以帮助我解决这个问题吗? 非常感谢!

该站点是动态的,因为它使用脚本来查询端点并使用返回的数据填充表。 因此,您可以使用selenium等浏览器操作工具访问页面或查询端点并自己解析 JSON 响应:

import requests, json
data = json.loads(requests.get('https://www.jma.go.jp/bosai/quake/data/list.json?__time__=202107300300').text)
result = [{'observed':i['at'], 'region':i['en_anm'], 'magnitude':i['mag'], 'max intensity':i['maxi']} for i in data]

输出( result前十行):

[{'observed': '2021-07-30T03:48:00+09:00', 'region': 'Off the Coast of Iwate Prefecture', 'magnitude': '3.7', 'max intensity': '1'}, {'observed': '2021-07-30T03:26:00+09:00', 'region': 'Southern Kyoto Prefecture', 'magnitude': '3.6', 'max intensity': '3'}, {'observed': '2021-07-30T03:26:00+09:00', 'region': 'Southern Kyoto Prefecture', 'magnitude': '3.6', 'max intensity': ''}, {'observed': '2021-07-30T03:26:00+09:00', 'region': '', 'magnitude': '', 'max intensity': '3'}, {'observed': '2021-07-29T21:22:00+09:00', 'region': 'Adjacent Sea of\u200b Chichijima Island', 'magnitude': '4.2', 'max intensity': '1'}, {'observed': '2021-07-29T21:17:00+09:00', 'region': 'Adjacent Sea of\u200b Chichijima Island', 'magnitude': '4.1', 'max intensity': '1'}, {'observed': '2021-07-29T18:57:00+09:00', 'region': 'Adjacent Sea of Tokara Islands', 'magnitude': '2.0', 'max intensity': '1'}, {'observed': '2021-07-29T18:52:00+09:00', 'region': 'Adjacent Sea of Tokara Islands', 'magnitude': '2.8', 'max intensity': '2'}, {'observed': '2021-07-29T15:16:00+09:00', 'region': 'Aleutian Islands', 'magnitude': '8.2', 'max intensity': ''}, {'observed': '2021-07-29T16:17:00+09:00', 'region': 'Off the Coast of Ibaraki Prefecture', 'magnitude': '4.1', 'max intensity': '1'}]

编辑:提取特定区域的数据:

vals = [i for i in result if 'Ibaraki Prefecture' in i['region']]

输出:

[{'observed': '2021-07-29T16:17:00+09:00', 'region': 'Off the Coast of Ibaraki Prefecture', 'magnitude': '4.1', 'max intensity': '1'}, {'observed': '2021-07-28T01:52:00+09:00', 'region': 'Southern Ibaraki Prefecture', 'magnitude': '3.4', 'max intensity': '1'}, {'observed': '2021-07-28T00:55:00+09:00', 'region': 'Off the Coast of Ibaraki Prefecture', 'magnitude': '4.5', 'max intensity': '3'}, {'observed': '2021-07-28T00:55:00+09:00', 'region': 'Off the Coast of Ibaraki Prefecture', 'magnitude': '4.5', 'max intensity': ''}, {'observed': '2021-07-27T13:39:00+09:00', 'region': 'Off the Coast of Ibaraki Prefecture', 'magnitude': '4.0', 'max intensity': '1'}, {'observed': '2021-07-23T09:59:00+09:00', 'region': 'Off the Coast of Ibaraki Prefecture', 'magnitude': '3.8', 'max intensity': '1'}, {'observed': '2021-07-23T03:15:00+09:00', 'region': 'Off the Coast of Ibaraki Prefecture', 'magnitude': '3.7', 'max intensity': '2'}, {'observed': '2021-07-20T15:56:00+09:00', 'region': 'Off the Coast of Ibaraki Prefecture', 'magnitude': '3.6', 'max intensity': '1'}, {'observed': '2021-07-15T05:17:00+09:00', 'region': 'Off the Coast of Ibaraki Prefecture', 'magnitude': '3.1', 'max intensity': '1'}, {'observed': '2021-07-04T15:35:00+09:00', 'region': 'Off the Coast of Ibaraki Prefecture', 'magnitude': '4.2', 'max intensity': '3'}, {'observed': '2021-07-04T15:35:00+09:00', 'region': 'Off the Coast of Ibaraki Prefecture', 'magnitude': '4.2', 'max intensity': ''}]

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM