scrapy / scrapy
1
"""
2
This module defines the Link object used in Link extractors.
3

4
For actual link extractors implementation see scrapy.linkextractors, or
5
its documentation in: docs/topics/link-extractors.rst
6
"""
7

8

9 7
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 7
    __slots__ = ['url', 'text', 'fragment', 'nofollow']
28

29 7
    def __init__(self, url, text='', fragment='', nofollow=False):
30 7
        if not isinstance(url, str):
31 7
            got = url.__class__.__name__
32 7
            raise TypeError(f"Link urls must be str objects, got {got}")
33 7
        self.url = url
34 7
        self.text = text
35 7
        self.fragment = fragment
36 7
        self.nofollow = nofollow
37

38 7
    def __eq__(self, other):
39 7
        return (
40
            self.url == other.url
41
            and self.text == other.text
42
            and self.fragment == other.fragment
43
            and self.nofollow == other.nofollow
44
        )
45

46 7
    def __hash__(self):
47 7
        return hash(self.url) ^ hash(self.text) ^ hash(self.fragment) ^ hash(self.nofollow)
48

49 7
    def __repr__(self):
50 7
        return (
51
            f'Link(url={self.url!r}, text={self.text!r}, '
52
            f'fragment={self.fragment!r}, nofollow={self.nofollow!r})'
53
        )

Read our documentation on viewing source code .

Loading