我正在为自己制作一个一次性的剧本,以便在周五和周六获得日落时间,以确定Shabbat和Havdalah何时开始。现在,我能够从timeanddate.com中删除时间 - 使用BeautifulSoup - 并将它们存储在列表中。不幸的是,我被困在那些时候;我想做的是能够减去或增加时间。由于Shabbat蜡烛照明时间是日落前18分钟,我希望能够在星期五拍摄给定的日落时间并从中减去18分钟。这是我到目前为止的代码:
import datetime
import requests
from BeautifulSoup import BeautifulSoup
# declare all the things here...
day = datetime.date.today().day
month = datetime.date.today().month
year = datetime.date.today().year
soup = BeautifulSoup(requests.get('http://www.timeanddate.com/worldclock/astronomy.html?n=43').text)
# worry not.
times = []
for row in soup('table',{'class':'spad'})[0].tbody('tr'):
tds = row('td')
times.append(tds[1].string)
#end for
shabbat_sunset = times[0]
havdalah_time = times[1]
到目前为止,我被困住了。时间[]中的对象显示为BeautifulSoup NavigatableStrings,我无法将其修改为整数(出于显而易见的原因)。任何帮助将不胜感激,谢谢你sososososo。
修改 所以,我使用了使用mktime并将BeautifulSoup的字符串变成常规字符串的建议。现在我得到一个OverflowError:当我在shabbat上调用mktime时,mktime超出范围......
for row in soup('table',{'class':'spad'})[0].tbody('tr'):
tds = row('td')
sunsetStr = "%s" % tds[2].text
sunsetTime = strptime(sunsetStr,"%H:%M")
shabbat = mktime(sunsetTime)
candlelighting = mktime(sunsetTime) - 18 * 60
havdalah = mktime(sunsetTime) + delta * 60
答案 0 :(得分:1)
您应该使用datetime.timedelta()函数。
例如:
time_you_want = datetime.datetime.now()+ datetime.timedelta(minutes = 18)
另见:
Python Create unix timestamp five minutes in the future
Shalom Shabbat
答案 1 :(得分:1)
我采用的方法是将完整的时间解析为正常表示 - 在Python世界中,这种表示是自Unix时代以来,1970年1月1日午夜的秒数。为此,您还需要查看第0列。(顺便说一下,tds [1]是日出时间,而不是我想要的。)
见下文:
#!/usr/bin/env python
import requests
from BeautifulSoup import BeautifulSoup
from time import mktime, strptime, asctime, localtime
soup = BeautifulSoup(requests.get('http://www.timeanddate.com/worldclock/astronomy.html?n=43').text)
# worry not.
(shabbat, havdalah) = (None, None)
for row in soup('table',{'class':'spad'})[0].tbody('tr'):
tds = row('td')
sunsetStr = "%s %s" % (tds[0].text, tds[2].text)
sunsetTime = strptime(sunsetStr, "%b %d, %Y %I:%M %p")
if sunsetTime.tm_wday == 4: # Friday
shabbat = mktime(sunsetTime) - 18 * 60
elif sunsetTime.tm_wday == 5: # Saturday
havdalah = mktime(sunsetTime)
print "Shabbat - 18 Minutes: %s" % asctime(localtime(shabbat))
print "Havdalah %s" % asctime(localtime(havdalah))
其次,帮助自己:'tds'列表是BeautifulSoup.Tag的列表。要获取有关此对象的文档,请打开Python终端,键入
import BeautifulSoup
help(BeautifulSoup.Tag)