美丽的汤4打印汤.find_all错误

时间:2015-04-18 14:51:51

标签: python beautifulsoup

我尝试将文字从美丽的汤中导出到文本文件中但显示

"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) 

怎么了?

1 个答案:

答案 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')