An element of HTML. :param element: The element from which to base the parsing upon. :param url: The URL from which the HTML originated, used for ``absolute_links``. :param default_encoding: Which encoding to default to.
| 363 | |
| 364 | |
| 365 | class Element(BaseParser): |
| 366 | """An element of HTML. |
| 367 | |
| 368 | :param element: The element from which to base the parsing upon. |
| 369 | :param url: The URL from which the HTML originated, used for ``absolute_links``. |
| 370 | :param default_encoding: Which encoding to default to. |
| 371 | """ |
| 372 | |
| 373 | __slots__ = [ |
| 374 | 'element', 'url', 'skip_anchors', 'default_encoding', '_encoding', |
| 375 | '_html', '_lxml', '_pq', '_attrs', 'session' |
| 376 | ] |
| 377 | |
| 378 | def __init__(self, *, element, url: _URL, default_encoding: _DefaultEncoding = None) -> None: |
| 379 | super(Element, self).__init__(element=element, url=url, default_encoding=default_encoding) |
| 380 | self.element = element |
| 381 | self.tag = element.tag |
| 382 | self.lineno = element.sourceline |
| 383 | self._attrs = None |
| 384 | |
| 385 | def __repr__(self) -> str: |
| 386 | attrs = ['{}={}'.format(attr, repr(self.attrs[attr])) for attr in self.attrs] |
| 387 | return "<Element {} {}>".format(repr(self.element.tag), ' '.join(attrs)) |
| 388 | |
| 389 | @property |
| 390 | def attrs(self) -> _Attrs: |
| 391 | """Returns a dictionary of the attributes of the :class:`Element <Element>` |
| 392 | (`learn more <https://www.w3schools.com/tags/ref_attributes.asp>`_). |
| 393 | """ |
| 394 | if self._attrs is None: |
| 395 | self._attrs = {k: v for k, v in self.element.items()} |
| 396 | |
| 397 | # Split class and rel up, as there are ussually many of them: |
| 398 | for attr in ['class', 'rel']: |
| 399 | if attr in self._attrs: |
| 400 | self._attrs[attr] = tuple(self._attrs[attr].split()) |
| 401 | |
| 402 | return self._attrs |
| 403 | |
| 404 | |
| 405 | class HTML(BaseParser): |