我试图将漂亮汤中的文本导出到文本文件中,但它显示了
"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)
怎么了?
发布于 2015-04-18 06:57:19
来自文档
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')
https://stackoverflow.com/questions/29718876
复制相似问题