我正在爲我自己的一次性腳本獲取週五和週六的日落時間,以確定何時安息日和哈弗達拉開始。現在,我可以從timeanddate.com上颳去時間 - 使用BeautifulSoup - 並將它們存儲在列表中。不幸的是,我陷入了那些時代。我想要做的是能夠減去或增加時間給他們。由於安息日蠟燭照明時間爲日落前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]
到目前爲止,我卡住了。 times []中的對象顯示爲BeautifulSoup NavigatableStrings,我無法修改爲整數(出於顯而易見的原因)。任何幫助將不勝感激,並感謝你sososososo了。
編輯 因此,我使用了使用mktime並將BeautifulSoup的字符串變成普通字符串的建議。現在,我發現了一個OverflowError: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
mktime返回它的參數和1月1日之間的秒數,1970年你沒有給它足夠的信息。 1970年1月1日到5:30 PM之間有多少秒鐘? – lyngvi