我正在使用
uj5u.com熱心網友回復:
這個腳本可能會有所幫助!我假設所有 .ASC 檔案都在一個名為“input_files”的檔案夾中,該檔案夾與腳本位于同一目錄中!您可以在以下腳本的第 3 行中更改所有 .ASC 檔案所在檔案夾的路徑。
import os
input_dir = "input_files"
output_file = "all_data.txt"
output_lst = []
def process_file(filepath):
interesting_keys = (
'Network',
'Station_Code',
'Station_Name',
'Station_Latitude_Degree',
'Station_Longitude_Degree',
'PGA_CM/S^2'
)
with open(filepath) as fh:
content = fh.readlines()
for line in content:
line = line.strip()
if ":" in line:
key, _ = line.split(":", 1)
if key.strip() in interesting_keys:
output_lst.append(line)
def write_output_data():
if output_lst:
with open(output_file, "w") as fh:
fh.write("\n".join(output_lst))
print("See", output_file)
def process_files():
for filepath in os.listdir(input_dir):
process_file(os.path.join(input_dir, filepath))
write_output_data()
process_files()
uj5u.com熱心網友回復:
這是一個有趣的,請參閱下面的腳本,它會下載所有原始資料。我已按要求提取了關鍵資料,但您可能想自己查看原始資料,因為那里有很多。
需要注意的一點是,我能找到的唯一“PGA_CM/S^2”資料是每條記錄的“開始”頁面上的粗體值。
import requests
import pandas as pd
url = 'https://esm-db.eu/esm_next_ws/jsonrpc'
payload = '{"jsonrpc":"2.0","method":"armonia","id":"8","params":{"map":{"_page":"DYNA_X_event_waveform_band_instrument_D","_state":"find","_action_json_rpc_list":"1","_rows_per_page":"10000","internal_event_id":"IT-2012-0008","_operator_internal_event_id":"=","_order_field_0":"epi_dist","_order_direction_0":"asc","_token":"NULLNULLNULLNULL"}}}'
con_len = len(payload)
headers= {
'Accept':'application/json, text/plain, */*',
'Accept-Encoding':'gzip, deflate, br',
'Content-Length':str(con_len),
'Content-Type':'application/x-www-form-urlencoded',
'Host':'esm-db.eu',
'Origin':'https://esm-db.eu',
'Referer':'https://esm-db.eu/',
'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/97.0.4692.71 Safari/537.36'
}
print('Fetching data...')
data = requests.post(url,headers=headers,data=payload).json()
final = []
for row in data['result']['rows']:
#loads of data, might be worth downloading json (data variable) and seeing what else you want
item = {
'Network':row[39],
'Station_Code':row[22],
'Station_Name':row[46][10],
'Station_Latitude_Degree':row[46][12],
'Station_Longitude_Degree':row[46][3],
'PGA_CM/S^2':row[18], #can only find the value in bold on the "Go" page
'Date':row[24].replace('T',' '),
}
final.append(item)
df = pd.DataFrame(final)
df.to_csv('earthquakedata.csv',index=False)
print('Saved to earthquake.csv')
如果您想要整個資料負載(在 csv 中幾乎無法管理),那么您可以通過將最后幾行更改為以下方式將其全部轉儲到 csv 中:
print('Fetching data...')
data = requests.post(url,headers=headers,data=payload).json()
df = pd.DataFrame(data['result']['rows'])
df.to_csv('earthquakedata_ugly.csv',index=False)
print('Saved to earthquake_ugly.csv')
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/414645.html
標籤:
