| 1 |
from __future__ import annotations |
| 2 |
|
| 3 |
from html.parser import HTMLParser |
| 4 |
|
| 5 |
|
| 6 |
def get_meta_description(meta_tags: str) -> bool: |
| 7 |
htp = HTMLTextParser() |
| 8 |
htp.feed(meta_tags) |
| 9 |
htp.close() |
| 10 |
|
| 11 |
return htp.meta_description |
| 12 |
|
| 13 |
|
| 14 |
class HTMLTextParser(HTMLParser): |
| 15 |
"""Parse HTML into text.""" |
| 16 |
|
| 17 |
def __init__(self) -> None: |
| 18 |
super().__init__() |
| 19 |
self.meta_description = None |
| 20 |
|
| 21 |
def handle_starttag(self, tag: str, attrs: list[tuple[str, str | None]]) -> None: |
| 22 |
# For example: |
| 23 |
# attrs = [("content", "My manual description"), ("name", "description")] |
| 24 |
if ('name', 'description') in attrs: |
| 25 |
self.meta_description = True |
| 26 |
for name, value in attrs: |
| 27 |
if name == 'content': |
| 28 |
self.meta_description = value |
| 29 |
break |