Get or set the text representation of sub nodes. Get the text value:: >>> doc = PyQuery(' toto tata ') >>> print(doc.text()) tototata >>> doc = PyQuery(''' toto ...
(self, value=no_default, **kwargs)
| 1155 | return etree.tostring(e0, encoding=str, method=method) |
| 1156 | |
| 1157 | def text(self, value=no_default, **kwargs): |
| 1158 | """Get or set the text representation of sub nodes. |
| 1159 | |
| 1160 | Get the text value:: |
| 1161 | |
| 1162 | >>> doc = PyQuery('<div><span>toto</span><span>tata</span></div>') |
| 1163 | >>> print(doc.text()) |
| 1164 | tototata |
| 1165 | >>> doc = PyQuery('''<div><span>toto</span> |
| 1166 | ... <span>tata</span></div>''') |
| 1167 | >>> print(doc.text()) |
| 1168 | toto tata |
| 1169 | |
| 1170 | Get the text value, without squashing newlines:: |
| 1171 | |
| 1172 | >>> doc = PyQuery('''<div><span>toto</span> |
| 1173 | ... <span>tata</span></div>''') |
| 1174 | >>> print(doc.text(squash_space=False)) |
| 1175 | toto |
| 1176 | tata |
| 1177 | |
| 1178 | Set the text value:: |
| 1179 | |
| 1180 | >>> doc.text('Youhou !') |
| 1181 | [<div>] |
| 1182 | >>> print(doc) |
| 1183 | <div>Youhou !</div> |
| 1184 | |
| 1185 | """ |
| 1186 | |
| 1187 | if value is no_default: |
| 1188 | if not self: |
| 1189 | return '' |
| 1190 | return ' '.join( |
| 1191 | self._copy(tag).html() if tag.tag == 'textarea' else |
| 1192 | extract_text(tag, **kwargs) for tag in self |
| 1193 | ) |
| 1194 | |
| 1195 | for tag in self: |
| 1196 | for child in tag.getchildren(): |
| 1197 | tag.remove(child) |
| 1198 | tag.text = value |
| 1199 | return self |
| 1200 | |
| 1201 | ################ |
| 1202 | # Manipulating # |