减去或加上网络垃圾时间

2024-09-28 21:21:15 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在为自己编写一个一次性的脚本,以获取星期五和星期六的日落时间,以便确定沙巴特和哈夫达拉何时开始。现在,我可以从时间与日期--使用beauthoulsoup——并将它们存储在一个列表中。不幸的是,我被这些时间困住了;我想做的是能够减少或增加时间。由于夏巴特蜡烛的照明时间是日落前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[]中的对象显示为beautifulsoupnavigatablestrings,我无法将其修改为int(原因很明显)。任何帮助都将不胜感激,非常感谢你。在

编辑 所以,我建议使用mktime并将beauthoulsoup的字符串转换为常规字符串。现在我得到一个溢出错误:mktime超出范围当我在shabbat上调用mktime。。。在

^{pr2}$

Tags: importtodaydatetimedate时间requestsyearsoup
2条回答

你应该用datetime.timedelta()功能。

例如:

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

另请参见:

Python Create unix timestamp five minutes in the future

沙洛姆沙巴特

我将采用的方法是将整个时间解析为一个正常的表示形式——在Python世界中,这个表示是自1970年1月1日午夜Unix时代以来的秒数。要执行此操作,还需要查看第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列表是一个列表美化组标签. 要获取此对象的文档,请打开Python终端,键入

^{cd1>}

相关问题 更多 >