2014-02-23 46 views
0

我正在爲我自己的一次性腳本獲取週五和週六的日落時間,以確定何時安息日和哈弗達拉開始。現在,我可以從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 
+0

mktime返回它的參數和1月1日之間的秒數,1970年你沒有給它足夠的信息。 1970年1月1日到5:30 PM之間有多少秒鐘? – lyngvi

回答

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)