我正在尝试根据这个日历创建一个小时字典:http: //disneyworld.disney.go.com/parks/magic-kingdom/calendar/
<td class="first"> <div class="dayContainer">
<a href="/parks/magic-kingdom/calendardayview/?asmbly_day=20120401">
<p class="day"> 1
</p> <p class="moreLink">Park Hours<br />8:00 AM - 12:00 AM<br /><br/>Extra Magic Hours<br />7:00 AM - 8:00 AM<br /><br/>Extra Magic Hours<br />12:00 AM - 3:00 AM<br /><br/>
</p>
</a>
</div>
</td>
每个日历条目都在一行上,所以我认为最好逐行浏览 HTML,如果该行包含小时,则将这些小时添加到相应日期的字典中(有些日子有多个小时条目)。
import urllib
import re
source = urllib.urlopen('http://disneyworld.disney.go.com/parks/magic-kingdom/c\
alendar/')
page = source.read()
prkhrs = {}
def main():
parsehours()
def parsehours():
#look for #:## AM - #:## PM
date = r'201204\d{02}'
hours = r'\d:0{2}\s\w{2}\s-\s\d:0{2}\s\w{2}'
#go through page line by line
for line in page:
times = re.findall(hours, line)
dates = re.search(date, line)
if dates:
start = dates.start()
end = dates.end()
curdate = line[start:end]
#if #:## - #:## is found, a date has been found
if times:
#create dictionary from date, stores hours in variable
#extra magic hours(emh) are stored in same format.
#if entry has 2/3 hour listings, those listings are emh
prkhrs[curdate]['hours'] = times
#just print hours for now. will change later
print prkhrs
我遇到的问题是,当我将“打印行”放在遍历页面的 for 循环中时,它一次打印出一个字符,我假设这是把事情搞砸了。
现在,'print prkhrs' 什么都不打印,但是使用 re.findall 来打印日期和时间会打印出正确的时间,所以我知道正则表达式有效。关于如何让它工作的任何建议?