Docs/API reference

ElementCollection · Python

Read, iterate and select from multiple DOM matches.

Download .md
Developers4 min read19 sections
Source reference · Match your SDK version. Availability and known issues
In this guide

Purpose and lifecycle

Node text/html properties return arrays. Python text() concatenates text, while texts() returns a list. Node attr(name) returns one value per match; Python attr(name) reads the first match.

Node indices are zero-based. first()/at() return null when no element exists; Python first()/last()/nth() return None.

Python filter/map run application callbacks in Python. Known issue: iteration over a materialized filtered collection currently exits without yielding its stored items; use texts() or explicit nth() access until corrected.

Definition and availability

Source snapshot: 1.0.0-alpha.3. Package export. 1 member(s) have a known limitation; see the marked entries below.

Signatures and source locations are generated from the SDK wrappers. Clean snapshots link to the recorded commit. Modified snapshots link to main, which may differ; file hashes identify the local inputs.

class ElementCollection
python1 lines · copy-ready

Usage example

Use a matching source build. Where a snippet uses page, obtain it with the Page example first. Example URLs illustrate API shape; they are not promised datasets.

links = page.css("a")
print(len(links), links.texts())
for link in links:
    print(link.attr("href"))
python4 lines · copy-ready

Members

MemberKindReturn / value typeSource status
__init__methodNot annotated; see behavior notesPresent in source
textmethodstrPresent in source
textsmethodList[str]Present in source
attrmethodstrPresent in source
attrsmethoddictPresent in source
firstmethodOptional[Element]Present in source
lastmethodOptional[Element]Present in source
nthmethodOptional[Element]Present in source
__getitem__methodOptional[Element]Present in source
__iter__methodIterator[Element]Known limitation — read entry
__len__methodintPresent in source
filtermethod'ElementCollection'Present in source
mapmethodlistPresent in source
__repr__methodstrPresent in source

__init__()

Wrap a native match collection or materialized Python Element list. See the known iteration limitation for materialized lists.

Returns / value: Not annotated; see behavior notes.

def __init__(self, core_collection: _CoreElementCollection=None, after_extract_hooks: List[Callable]=None, items: List[Element]=None)
python1 lines · copy-ready
ParameterTypeRequired / defaultMeaning
core_collection_CoreElementCollectionDefault: NoneNative collection of matched DOM elements.
after_extract_hooksList[Callable]Default: NonePython string callbacks applied after text extraction. Empty return values retain the previous string.
itemsList[Element]Default: NoneMaterialized Python Element list. Read the iteration limitation before using it.

text()

Node: array of each match’s text. Python: concatenated text for the collection.

Returns / value: str.

def text(self) -> str
python1 lines · copy-ready

texts()

Return each selected element’s text as a Python list.

Returns / value: List[str].

def texts(self) -> List[str]
python1 lines · copy-ready

attr()

Node: array of attribute values or null. Python: attribute of the first match, or None.

Returns / value: str.

def attr(self, name: str) -> str
python1 lines · copy-ready
ParameterTypeRequired / defaultMeaning
namestrRequiredField, header or attribute name for this operation.

attrs()

Return attributes of the first Python match, or an empty dictionary.

Returns / value: dict.

def attrs(self) -> dict
python1 lines · copy-ready

first()

Return the first match, or null/None.

Returns / value: Optional[Element].

def first(self) -> Optional[Element]
python1 lines · copy-ready

last()

Return the final match, or None.

Returns / value: Optional[Element].

def last(self) -> Optional[Element]
python1 lines · copy-ready

nth()

Return a zero-based match, or None if out of bounds.

Returns / value: Optional[Element].

def nth(self, n: int) -> Optional[Element]
python1 lines · copy-ready
ParameterTypeRequired / defaultMeaning
nintRequiredCount or index; see the method description.

__getitem__()

Delegate to nth(index). Use a non-negative index; out-of-range matches return None.

Returns / value: Optional[Element].

def __getitem__(self, index: int) -> Optional[Element]
python1 lines · copy-ready
ParameterTypeRequired / defaultMeaning
indexintRequiredZero-based element index.

__iter__()

Known limitation: For a materialized collection (for example filter() output), this generator returns an iterator instead of yielding from it. Iteration/map can produce no items.

Iterate over matched Element wrappers in collection order. See any known limitation above before using a materialized Python collection.

Returns / value: Iterator[Element].

def __iter__(self) -> Iterator[Element]
python1 lines · copy-ready

__len__()

Return the number of stored results or matched elements; no additional pages are fetched.

Returns / value: int.

def __len__(self) -> int
python1 lines · copy-ready

filter()

Build a materialized Python collection from elements satisfying the predicate. See the iteration limitation above.

Returns / value: 'ElementCollection'.

def filter(self, fn: Callable[[Element], bool]) -> 'ElementCollection'
python1 lines · copy-ready
ParameterTypeRequired / defaultMeaning
fnCallable[[Element], bool]RequiredApplication callback invoked with the documented value or event.

map()

Return a Python list of callback results; see the materialized-collection iteration limitation above.

Returns / value: list.

def map(self, fn: Callable[[Element], any]) -> list
python1 lines · copy-ready
ParameterTypeRequired / defaultMeaning
fnCallable[[Element], any]RequiredApplication callback invoked with the documented value or event.

__repr__()

Return a short debugging representation of the wrapper. This is not a data export or a stable serialization format.

Returns / value: str.

def __repr__(self) -> str
python1 lines · copy-ready

Errors and operational limits

An empty match is valid. Reject negative indices in your Node caller; the native index type is unsigned. Callback exceptions can propagate from Python map/filter.

Source availability, package publication and runtime validation are separate facts. These pages describe the source snapshot, not a claim that all platforms or every API path have passed production acceptance.