AttributeError:'ResultSet'对象没有'编码'属性
问题描述:
我想从url中提取数据,但在写文件时我得到这个错误,因为text
不为空。AttributeError:'ResultSet'对象没有'编码'属性
我的代码:
def gettextonly(self, url):
url = url
html = urllib.urlopen(url).read()
soup = BeautifulSoup(html)
# kill all script and style elements
for script in soup(["script", "style","a","<div id=\"bottom\" >"]):
script.extract() # rip it out
text = soup.findAll(text=True)
#print text
fo = open('foo.txt', 'w')
fo.seek(0, 2)
if text:
line =fo.writelines(text.encode('utf8'))
fo.close()
错误:
in gettextonly
line =fo.writelines(text.encode('utf8'))
AttributeError: 'ResultSet' object has no attribute 'encode'
答
soup.findAll(text=True)
返回ResultSet
对象,它是基本上不具有的属性encode
列表。要么你想用的是.text
代替:
text = soup.text
或者说, “加入” 的文本:
text = "".join(soup.findAll(text=True))