首页 > 解决方案 > 如何从 HTML 表创建 ical 文件

问题描述

我正在尝试从网站创建可导入的日历事件。该网站将事件聚集到一个标准的 html 表中。

我想知道 beautfulsoup 是否是解决这个问题的正确方法,因为我只得到第一个条目,然后什么也没有。

quote_page = "http://www.ellen-hartmann.de/babybasare.html"

page = urllib2.urlopen(quote_page)

soup = BeautifulSoup(page, "html.parser")

table = soup.find("table", {"border": "1"})

td = table.find("td", text="Veranstaltungstyp ")

print table

td_next = table.find_next("tr")

print td_next

标签: pythonhtmlbeautifulsoupicalendar

解决方案


我认为您正在停止使用,因为您使用find()which 获得一个匹配的标签,而不是find_all()获得所有匹配的标签。然后你必须遍历结果

import requests
from bs4 import BeautifulSoup

response = requests.get("http://www.ellen-hartmann.de/babybasare.html")

soup = BeautifulSoup(response.text, 'html.parser')

# now let's find every row in every table
for row in soup.find_all("tr"):

    # grab the cells within the row
    cells = row.find_all("td")

    # print the value of the cells as a list.  This is the point where
    # you will need to filter the rows to figure out what is an event (and
    # what is not), determine the start date and time, and convert the values 
    # to iCal format.
    print([c.text for c in cells])


推荐阅读