簡體   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