如何查找过去 24 小时的信息
我是编程初学者,我想找到最简单的方法来做到这一点。我有一条信息,其中有一行显示它何时以 UTC 时间更新,现在我想编写程序来询问信息是否在过去 24 小时内,而不是打印信息。我正在使用 Python 3,谢谢
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)
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)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
查看美国地质调查局提供的数据,他们似乎已经向您提供了此信息。例如,在第一个条目中,我看到
,而最后一个条目是
。假设您信任此信息,您可以像提取标题和时间一样提取它:
然后您可以根据年龄是否为
“过去一天”
来过滤数据。更新:以下是手动检查日期的方法。
请注意
strptime()
< /a> 需要时区,因此您应该从findQuakeTime()
代码中删除-3
。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-3
from yourfindQuakeTime()
code.有两种可能的方法:
将信息中的每一行转换为本地时间并与当前时间进行比较。
获取当前 UTC 时间并与信息中的元数据进行比较。在我看来,该解决方案更简洁,并且在显示多个项目时性能会更好,因为只需要一次转换。
除非您提供有关您正在使用的编程语言、平台和任何框架的更多信息,否则无法提供更详细的帮助。
编辑:
现在我们知道您正在使用Python(您还应该提到它是Python-3,而不是当前的稳定版本),并且因为您的
quakeTime
变量似乎采用以下形式 < code>2011 年 3 月 7 日星期二 15:52:08,这是一个小脚本,用于测试给定日期是否在过去 24 小时内:以及几个用例:
There are two possible approaches:
Convert each line in your information to your local time and compare with the current time.
Get the current time in UTC and compare with the metadata in your information. In my opinion this solutions is cleaner and it will perform better when showing multiple items since only one conversion is necessary.
Unless you supply more information on your programming language, platform and any frameworks that you are using, there is no way to provide more detailed help.
EDIT:
Now that we know that your are using Python (and you should also mention that it is Python-3, rather than the current stable version) and since your
quakeTime
variable seems to be in the formTuesday, March 7, 2011 15:52:08
, here's a small script that tests if a given date is within the last 24 hours or not:And a couple use cases: