0

I'm working on a one-off script for myself to get sunset times for Friday and Saturday, in order to determine when Shabbat and Havdalah start. Now, I was able to scrape the times from timeanddate.com -- using BeautifulSoup -- and store them in a list. Unfortunately, I'm stuck with those times; what I would like to do is be able to subtract or add time to them. As Shabbat candle-lighting time is 18 minutes before sunset, I'd like to be able to take the given sunset time for Friday and subtract 18 minutes from it. Here is the code I have thus far:

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]

So far, I'm stuck. The objects in times[] are shown to be BeautifulSoup NavigatableStrings, which I can't modify into ints (for obvious reasons). Any help would be appreciated, and thank you sososososo much.

EDIT So, I used the suggestion of using mktime and making BeautifulSoup's string into a regular string. Now I'm getting an OverflowError: mktime out of range when I call mktime on shabbat...

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
bluebunny
  • 295
  • 3
  • 13
  • mktime returns the number of seconds between its argument and 1 Jan, 1970. You didn't give it enough information. How many seconds are between 1 Jan 1970 and 5:30 PM? – lyngvi Feb 23 '14 at 15:32

2 Answers2

1

You should use the datetime.timedelta() function.

In example:

time_you_want = datetime.datetime.now() + datetime.timedelta(minutes = 18)

Also see here:

Python Create unix timestamp five minutes in the future

Shalom Shabbat

Community
  • 1
  • 1
Gi0rgi0s
  • 1,757
  • 2
  • 22
  • 31
1

The approach I'd take is to parse the complete time into a normal representation - in Python world, this representation is the number of seconds since the Unix epoch, 1 Jan 1970 midnight. To do this, you also need to look at column 0. (Incidentally, tds[1] is the sunrise time, not what I think you want.)

See below:

#!/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))

Second, help to help yourself: The 'tds' list is a list of BeautifulSoup.Tag. To get documentation on this object, open a Python terminal, type

import BeautifulSoup help(BeautifulSoup.Tag)

lyngvi
  • 1,312
  • 12
  • 19