我尝试将文字从美丽的汤中导出到文本文件中但显示
"text_file2.write(important)
TypeError: expected a character buffer object"
这是我的代码
important=soup.find_all("tr", class_="accList")
with open("important.txt","w") as text_file2:
text_file2.write(important)
怎么了?
答案 0 :(得分:1)
来自docs:
soup.find_all('a')
# [<a class="sister" href="http://example.com/elsie" id="link1">Elsie</a>,
# <a class="sister" href="http://example.com/lacie" id="link2">Lacie</a>,
# <a class="sister" href="http://example.com/tillie" id="link3">Tillie</a>]
因此,soup.find_all
返回list
,但您需要一个字符串(一个字符缓冲区对象)。
尝试以下方法:
with open("important.txt","w") as text_file2:
for x in important:
text_file2.write(str(x)+'\n')