I’m a beginner in programming and I want to find the easiest way of doing this. I have information which has a line in it that shows when it was updated in UTC time, now i want to write my program to ask if info is within the last 24 hours than print the info. I’m working in Python 3, Thanks
import datetime
import urllib.request
def findEarthquake(entry):
start= entry.find("<titile>") +7
end= entry.find("</title>")
eq= entry[start:end]
return eq
def findQuakeTime(entry):
start= entry.find("<p>") +3
end= entry.find("<br>") -3
time=entry[start:end]
return time
page= urllib.request.urlopen("http://earthquake.usgs.gov/eqcenter/catalogs/7day-M2.5.xml")
text= page.read().decode("utf8")
start= text.find("<entry>") +7
earthquakeList=[]
while start >= 0:
end= text.find("</entry>", start)
entry= text[start:end]
quake= findEarthquake(entry)
quakeTime= findQuakeTime(entry)
Looking at the data being provided by USGS, it seems they already provide this info to you. For example, in the first entry I see
<category label="Age" term="Past day"/>, while the last is<category label="Age" term="Past week"/>.Assuming you trust this information, you can extract it just as you do the title and time:
Then you can filter your data based on whether the age is
"Past day"or not.Update: And here’s how to check the dates manually.
Note that
strptime()wants the time zone, so you should remove the-3from yourfindQuakeTime()code.