BeautifulSoup : TypeError: 'unicode' object is not callable

BeautifulSoup : TypeError: 'unicode' object is not callable

这是我的代码:

v_card = soup.find('div', {'class':'col subgroup vcard'})
if v_card is not None :
    print v_card.prettify()
    infos = v_card.findAll('li')
    print infos[0].text()

这是输出:

<div class="col subgroup vcard">
<ul>
 <li>
  infos I need to get
 </li>
 <li>
  infos I need to get
 </li>
 <li>
 </li>
</ul>
</div>

Traceback (most recent call last):
  File "./xxxx.py", line 43, in <module>
    print infos[0].text()
TypeError: 'unicode' object is not callable

请注意,如果我删除 .text() 方法,那么它会成功打印 <li> 标签及其内容。

这很奇怪,因为对于其他元素我使用 .text() 没问题,我不明白,有什么解释吗?

.text是一个属性,返回节点包含的文本。不可调用,直接使用即可:

print infos[0].text

您可能对此处的 Element.get_text() method 感到困惑;访问 .text 属性与不带任何参数调用 .get_text() 基本相同。