2020-12-08 04:26:30 +01:00
|
|
|
import cgi
|
|
|
|
from typing import Any, Optional
|
2016-10-27 12:06:44 +02:00
|
|
|
|
2020-06-11 00:54:34 +02:00
|
|
|
|
2017-11-05 11:37:41 +01:00
|
|
|
class BaseParser:
|
2020-12-08 04:26:30 +01:00
|
|
|
def __init__(self, html_source: bytes, content_type: Optional[str]) -> None:
|
2018-08-08 22:24:20 +02:00
|
|
|
# We import BeautifulSoup here, because it's not used by most
|
|
|
|
# processes in production, and bs4 is big enough that
|
|
|
|
# importing it adds 10s of milliseconds to manage.py startup.
|
|
|
|
from bs4 import BeautifulSoup
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2020-12-08 04:26:30 +01:00
|
|
|
charset = None
|
|
|
|
if content_type is not None:
|
|
|
|
charset = cgi.parse_header(content_type)[1].get("charset")
|
|
|
|
self._soup = BeautifulSoup(html_source, "lxml", from_encoding=charset)
|
2016-10-27 12:06:44 +02:00
|
|
|
|
2017-11-05 11:15:10 +01:00
|
|
|
def extract_data(self) -> Any:
|
2017-05-24 02:39:38 +02:00
|
|
|
raise NotImplementedError()
|