-2

Here's my code :

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

Here's the output :

<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

Note that if I remove the .text() method, then it prints successfully the <li> tag and its content.

This is weird because with other elements I have no problem using .text(), I don't get it, any explanations ?

Loïc
  • 11,804
  • 1
  • 31
  • 49

1 Answers1

4

.text is an attribute, returning the contained text of the node. It is not callable, just use it directly:

print infos[0].text

You may have gotten confused with the Element.get_text() method here; accessing the .text attribute is basically the same thing as calling .get_text() without any arguments.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343