2017-07-19 60 views
-1

我將「example.txt」文件讀入名爲「lines」的列表中。如何讓我的文件適合BeautifulSoup?

現在我想用BeautifulSoup: parsed = BeautifulSoup(lines.content, "lxml")解析它,但它拋出一個錯誤:「名單」對象有沒有屬性「內容」

我該如何解決?

回答

0

你應該爲它提供一個包含文件內容的字符串。

parsed = BeautifulSoup(open('example.txt').read(), "lxml") 
0

您將文件讀入列表中,並且列表中沒有屬性內容。美麗的湯期待字符串,所以通過使用str.join將線摺疊爲字符串。

parsed = BeautifulSoup("".join(lines),"lxml")