Matches a single element by its index:: >>> from pyquery import PyQuery >>> d = PyQuery(' ') >>> d('h1:eq(0)') [ ] >>> d('h1:eq(1)') [ ] ..
(self, xpath, function)
| 366 | return xpath |
| 367 | |
| 368 | def xpath_eq_function(self, xpath, function): |
| 369 | """Matches a single element by its index:: |
| 370 | |
| 371 | >>> from pyquery import PyQuery |
| 372 | >>> d = PyQuery('<div><h1 class="first"/><h1 class="last"/></div>') |
| 373 | >>> d('h1:eq(0)') |
| 374 | [<h1.first>] |
| 375 | >>> d('h1:eq(1)') |
| 376 | [<h1.last>] |
| 377 | |
| 378 | .. |
| 379 | """ |
| 380 | if function.argument_types() != ['NUMBER']: |
| 381 | raise ExpressionError( |
| 382 | "Expected a single integer for :eq(), got %r" % ( |
| 383 | function.arguments,)) |
| 384 | value = int(function.arguments[0].value) |
| 385 | xpath.add_post_condition('position() = %s' % (value + 1)) |
| 386 | return xpath |
| 387 | |
| 388 | def xpath_gt_function(self, xpath, function): |
| 389 | """Matches all elements with an index over the given one:: |
nothing calls this directly
no test coverage detected