This commit is contained in:
2026-03-31 15:17:05 -04:00
parent 9996d673f4
commit 6b8a100ad0
2 changed files with 32 additions and 19 deletions

View File

@@ -1,6 +1,18 @@
import requests
import time
from bs4 import BeautifulSoup
import json
import os
def printQuake(properties):
magnitude = properties["mag"]
location = properties["place"]
timestamp = properties["time"] / 1000
time_str = time.strftime("%H:%M:%S", time.gmtime(timestamp))
date_str = time.strftime("%a, %d %b %Y", time.gmtime(timestamp))
print(f"At {time_str} on {date_str}: earthquake of magnitude {magnitude} at {location}")
def fetchAndPrintQuakes(start_date, end_date, min_magnitude):
@@ -18,32 +30,26 @@ def fetchAndPrintQuakes(start_date, end_date, min_magnitude):
data = response.json()
for quake in data["features"]:
properties = quake["properties"]
magnitude = properties["mag"]
location = properties["place"]
timestamp = properties["time"] / 1000
time_str = time.strftime("%H:%M:%S", time.gmtime(timestamp))
date_str = time.strftime("%a, %d %b %Y", time.gmtime(timestamp))
print(f"At {time_str} on {date_str}: earthquake of magnitude {magnitude} at {location}")
printQuake(quake["properties"])
def printAllLinks(url):
response = requests.get(url)
soup = BeautifulSoup(response.text, "html.parser")
def readAndPrintQuakes(filename):
script_dir = os.path.dirname(__file__)
filepath = os.path.join(script_dir, filename)
with open(filepath, "r") as f:
data = json.load(f)
for link in soup.find_all("a"):
print(link.get("href"))
for quake in data["features"]:
printQuake(quake["properties"])
def main():
print("Earthquakes:")
print("Earthquakes from API:")
fetchAndPrintQuakes("2023-10-01", "2023-10-03", 5.0)
print("\nLinks from example.com:")
printAllLinks("https://example.com")
print("\nEarthquakes from saved JSON:")
readAndPrintQuakes("load.json")
if __name__ == "__main__":