|
2 | 2 | import logging |
3 | 3 | import os |
4 | 4 | import re |
| 5 | +from html import unescape |
| 6 | +from html.parser import HTMLParser |
5 | 7 |
|
6 | 8 | import aiohttp |
7 | 9 |
|
|
10 | 12 | logger = logging.getLogger(__name__) |
11 | 13 |
|
12 | 14 |
|
| 15 | +class TextExtractor(HTMLParser): |
| 16 | + """ |
| 17 | + HTML parser for text extraction. |
| 18 | +
|
| 19 | + Extracts visible text content from an HTML string, excluding the contents of |
| 20 | + tags specified in _skip_tags. |
| 21 | + """ |
| 22 | + def __init__(self): |
| 23 | + super().__init__() |
| 24 | + self._parts = [] |
| 25 | + self._skip = False |
| 26 | + self._skip_tags = {"script", "style", "template"} |
| 27 | + |
| 28 | + def handle_starttag(self, tag, attrs): |
| 29 | + """ |
| 30 | + Marks the parser to skip content inside tags specified in _skip_tags. |
| 31 | +
|
| 32 | + Args: |
| 33 | + tag (str): The tag name. |
| 34 | + attrs (list): A list of (attribute, value) pairs. |
| 35 | + """ |
| 36 | + if tag in self._skip_tags: |
| 37 | + self._skip = True |
| 38 | + |
| 39 | + def handle_endtag(self, tag): |
| 40 | + """ |
| 41 | + Marks the parser the end of skip tags. |
| 42 | +
|
| 43 | + Args: |
| 44 | + tag (str): The tag name. |
| 45 | + """ |
| 46 | + if tag in self._skip_tags: |
| 47 | + self._skip = False |
| 48 | + |
| 49 | + def handle_data(self, data): |
| 50 | + """ |
| 51 | + Handles text nodes. Adds them to the result unless they are within a skip tag. |
| 52 | +
|
| 53 | + Args: |
| 54 | + data (str): The text data. |
| 55 | + """ |
| 56 | + if not self._skip: |
| 57 | + self._parts.append(unescape(data)) |
| 58 | + |
| 59 | + def get_strings(self, strip: bool): |
| 60 | + """ |
| 61 | + Yields all collected visible text fragments. |
| 62 | +
|
| 63 | + Args: |
| 64 | + strip (bool): Whether to strip leading/trailing whitespace from each fragment. |
| 65 | +
|
| 66 | + Yields: |
| 67 | + str: Visible text fragments. |
| 68 | + """ |
| 69 | + for text in self._parts: |
| 70 | + yield text.strip() if strip else text |
| 71 | + |
| 72 | + def get_text(self, separator: str, strip: bool) -> str: |
| 73 | + """ |
| 74 | + Returns all visible text. |
| 75 | +
|
| 76 | + Args: |
| 77 | + separator (str): String inserted between extracted text fragments. |
| 78 | + strip (bool): Whether to strip whitespace from each fragment. |
| 79 | +
|
| 80 | + Returns: |
| 81 | + str: The visible text. |
| 82 | + """ |
| 83 | + return separator.join(self.get_strings(strip=strip)) |
| 84 | + |
| 85 | + |
| 86 | +def extract_text_from_html(html: str, separator: str = '', strip: bool = False) -> str: |
| 87 | + """ |
| 88 | + Extracts visible text content from an HTML string. |
| 89 | +
|
| 90 | + Args: |
| 91 | + html (str): The HTML string to extract text from. |
| 92 | + separator (str, optional): String inserted between extracted text fragments. Defaults to ''. |
| 93 | + strip (bool, optional): Whether to strip whitespace from each text fragment. Defaults to False. |
| 94 | +
|
| 95 | + Returns: |
| 96 | + str: The extracted visible text. |
| 97 | + """ |
| 98 | + parser = TextExtractor() |
| 99 | + parser.feed(html) |
| 100 | + return parser.get_text(separator=separator, strip=strip) |
| 101 | + |
| 102 | + |
13 | 103 | def decode_base64_to_bytes(image: str) -> bytes: |
14 | 104 | """ |
15 | 105 | Decodes a base64 image string to bytes. |
|
0 commit comments