Link objects represent an extracted link by the LinkExtractor. Using the anchor tag sample below to illustrate the parameters:: <a href="https://example.com/nofollow.html#foo" rel="nofollow">Dont follow this one</a> :param url: the absolute url being linked to in the anchor ta
| 7 | |
| 8 | |
| 9 | class Link: |
| 10 | """Link objects represent an extracted link by the LinkExtractor. |
| 11 | |
| 12 | Using the anchor tag sample below to illustrate the parameters:: |
| 13 | |
| 14 | <a href="https://example.com/nofollow.html#foo" rel="nofollow">Dont follow this one</a> |
| 15 | |
| 16 | :param url: the absolute url being linked to in the anchor tag. |
| 17 | From the sample, this is ``https://example.com/nofollow.html``. |
| 18 | |
| 19 | :param text: the text in the anchor tag. From the sample, this is ``Dont follow this one``. |
| 20 | |
| 21 | :param fragment: the part of the url after the hash symbol. From the sample, this is ``foo``. |
| 22 | |
| 23 | :param nofollow: an indication of the presence or absence of a nofollow value in the ``rel`` attribute |
| 24 | of the anchor tag. |
| 25 | """ |
| 26 | |
| 27 | __slots__ = ["fragment", "nofollow", "text", "url"] |
| 28 | |
| 29 | def __init__( |
| 30 | self, url: str, text: str = "", fragment: str = "", nofollow: bool = False |
| 31 | ): |
| 32 | if not isinstance(url, str): |
| 33 | got = url.__class__.__name__ |
| 34 | raise TypeError(f"Link urls must be str objects, got {got}") |
| 35 | self.url: str = url |
| 36 | self.text: str = text |
| 37 | self.fragment: str = fragment |
| 38 | self.nofollow: bool = nofollow |
| 39 | |
| 40 | def __eq__(self, other: object) -> bool: |
| 41 | if not isinstance(other, Link): |
| 42 | raise NotImplementedError |
| 43 | return ( |
| 44 | self.url == other.url |
| 45 | and self.text == other.text |
| 46 | and self.fragment == other.fragment |
| 47 | and self.nofollow == other.nofollow |
| 48 | ) |
| 49 | |
| 50 | def __hash__(self) -> int: |
| 51 | return ( |
| 52 | hash(self.url) ^ hash(self.text) ^ hash(self.fragment) ^ hash(self.nofollow) |
| 53 | ) |
| 54 | |
| 55 | def __repr__(self) -> str: |
| 56 | return ( |
| 57 | f"Link(url={self.url!r}, text={self.text!r}, " |
| 58 | f"fragment={self.fragment!r}, nofollow={self.nofollow!r})" |
| 59 | ) |
no outgoing calls