我想從這個時間表採取某一天的數據:click here解析HTML表BeautifulSoup
我已經能夠用美麗的湯加入一整行的任何一天(在這種情況下, ,週一或「星期一」)使用該代碼的列表:
from BeautifulSoup import BeautifulSoup
day ='Mon'
with open('timetable.txt', 'rt') as input_file:
html = input_file.read()
soup = BeautifulSoup(html)
#finds correct day tag
starttag = soup.find(text=day).parent.parent
print starttag
nexttag = starttag
row=[]
x = 0
#puts all td tags for that day in a list
while x < 18:
nexttag = nexttag.nextSibling.nextSibling
row.append(nexttag)
x += 1
print row
,你可以看到,該命令返回TD標籤,從而彌補了「星期一」排時間表的列表。
我的問題是,我不知道如何進一步解析或搜索返回的列表來查找相關信息(COMP1740等)。
如果我可以找到如何搜索列表中的每個元素的模塊代碼,我可以將它們與另一個時序列表連接在一起,給出一天的時間表。
歡迎所有幫助! (包括完全不同的方法)
非常感謝您的幫助。原來只有我的模塊代碼以'COMP'開始,所以我只是將搜索模式改爲'rowspan =「1」',因爲那是代碼中唯一另外一件事情,它在表格中的那個位置提供了一個模塊。我將發佈新代碼作爲答案。 –
@Ben,關於你的新答案:當你過去最後一個兄弟姐妹時,nexttag將是None,所以你可以說'''如果不是nexttag:break'''。它比try/catch更清潔。 – FakeRainBrigand