| 3054 | return self.url > link.url |
| 3055 | |
| 3056 | class HTMLLinkParser(HTMLParser): |
| 3057 | |
| 3058 | def __init__(self): |
| 3059 | HTMLParser.__init__(self) |
| 3060 | |
| 3061 | def parse(self, html, url=""): |
| 3062 | """ Returns a list of Links parsed from the given HTML string. |
| 3063 | """ |
| 3064 | if html is None: |
| 3065 | return None |
| 3066 | self._url = url |
| 3067 | self._data = [] |
| 3068 | self.feed(self.clean(html)) |
| 3069 | self.close() |
| 3070 | self.reset() |
| 3071 | return self._data |
| 3072 | |
| 3073 | def handle_starttag(self, tag, attributes): |
| 3074 | if tag == "a": |
| 3075 | attributes = dict(attributes) |
| 3076 | if "href" in attributes: |
| 3077 | link = Link(url = attributes.get("href"), |
| 3078 | text = attributes.get("title"), |
| 3079 | relation = attributes.get("rel", ""), |
| 3080 | referrer = self._url) |
| 3081 | self._data.append(link) |
| 3082 | |
| 3083 | def base(url): |
| 3084 | """ Returns the URL domain name: |