對於正在被解析並放入列表中的字典的HTML,我知道<th>Name</th>
對應一個值。在BeautifulSoup中訪問包含標籤類型的字典
它似乎是拋出一個關鍵的錯誤,因爲它是一個標記,而不是一個字符串。
courseCode = "BSB119"
page = requests.get("https://www.qut.edu.au/study/unit?unitCode=" + courseCode)
soup = BeautifulSoup(page.content, 'html.parser')
tables = soup.find_all(class_='table assessment-item')
numOfTables = 0
list_ = []
dictionary_ = {}
for tbl in tables:
numOfTables = numOfTables + 1
keys = tbl.find_all('th').string
values = tbl.find_all('td').string
new_data = dict(zip(keys, values))
list_.append(new_data)
for i in range(0,numOfTables):
print(list_[i]["<th>Name</th>"])
如何將標記轉換爲字符串,以便我可以將其作爲字典鍵訪問?我嘗試了上面的方法,但它說我訪問超過一件事......
有一個在您所提供的任何表。 – Aaron
不知道爲什麼它不工作...它應該工作..現在嘗試它(我已經改變了課程代碼) – yeeeeee
它返回的是'[0.8s完成]' – yeeeeee