fixed
This commit is contained in:
44
HW 4B/HW4.py
44
HW 4B/HW4.py
@@ -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__":
|
||||
|
||||
Reference in New Issue
Block a user