Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: get charset from HTML if not in HTTP header #33

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions webpreview/parsers.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,32 @@ def make_absolute_url(url: str, base_url: str) -> Optional[str]:
return urlunparse(url_components)


def extract_charset(html: str) -> str:
"""Tries to extract the charset from the html"""

# Regex patterns to extract charset value
charset_pattern = r'<meta\s+charset=["\']([^"\']+)["\']'
content_charset_pattern = (
r'<meta\s+(?:[^>]*?http-equiv=["\']'
r'Content-Type["\'][^>]*?|[^>]*?content=["\']'
r'[^>]*?charset=([^"\'>]+)[^>]*?){2,}'
)

# Try to find <meta charset="...">
rematch = re.search(charset_pattern, html, re.IGNORECASE)
if rematch:
return rematch.group(1)

# Try to find <meta http-equiv="Content-Type" content="...charset=...">
rematch = re.search(content_charset_pattern, html, re.IGNORECASE)
if rematch:
charset_match = re.search(r'charset=([^"\';]+)', rematch.group(0), re.IGNORECASE)
if charset_match:
return charset_match.group(1)

return "utf-8" # assume utf-8 if not present


def retrieve_content(
url: str, timeout: Optional[int] = None, headers: Optional[Dict[str, str]] = None
) -> str:
Expand All @@ -157,6 +183,13 @@ def retrieve_content(
if res.status_code == 404:
raise URLNotFound("The web page does not exist.")

if "charset" not in res.headers["Content-Type"]:
try:
encoding = extract_charset(res.text)
return res.content.decode(encoding)
except:
pass

return res.text


Expand Down