| 24 | |
| 25 | |
| 26 | class Parser(HTMLParser): |
| 27 | def __init__(self, domain: str) -> None: |
| 28 | super().__init__() |
| 29 | self.urls: list[str] = [] |
| 30 | self.domain = domain |
| 31 | |
| 32 | def handle_starttag(self, tag: str, attrs: list[tuple[str, str | None]]) -> None: |
| 33 | """ |
| 34 | This function parse html to take takes url from tags |
| 35 | """ |
| 36 | # Only parse the 'anchor' tag. |
| 37 | if tag == "a": |
| 38 | # Check the list of defined attributes. |
| 39 | for name, value in attrs: |
| 40 | # If href is defined, not empty nor # print it and not already in urls. |
| 41 | if name == "href" and value not in (*self.urls, "", "#"): |
| 42 | url = parse.urljoin(self.domain, value) |
| 43 | self.urls.append(url) |
| 44 | |
| 45 | |
| 46 | # Get main domain name (example.com) |