1

我正在尝试解析包含多个事件的提要,并且它只返回一个项目

ics = urllib.urlopen("https://www.google.com/calendar/ical/pcolalug%40gmail.com/public/basic.ics").read()
events = []

components = vobject.readComponents(ics)
for event in components:
    to_zone = tz.gettz('America/Chicago')

    date = event.vevent.dtstart.value.astimezone(to_zone)
    description = event.vevent.description.value

    events.append({
                'start': date.strftime(DATE_FORMAT),
                'description': description if description else 'No Description', 
                })

return {'events': events[:10]}

我究竟做错了什么?

4

1 回答 1

2

改用 icalendar 而不是 vobject,效果好多了。

ics = urllib.urlopen("https://www.google.com/calendar/ical/pcolalug%40gmail.com/public/basic.ics").read()
events = []

cal = Calendar.from_string(ics)

for event in cal.walk('vevent'):
    to_zone = tz.gettz('America/Chicago')

    date = event.get('dtstart').dt.astimezone(to_zone)
    description = event.get('description')

    events.append({
                'start': date.strftime(DATE_FORMAT),
                'description': description if description else 'No Description', 
                })

return {'events': events[:10]}
于 2011-10-06T04:06:19.690 回答