given the above can you fix my code? South Africa’s it should be South Africa's
def parse_summary_paragraph(html_content: str) -> str | None:
soup = BeautifulSoup(html_content, "html.parser")
container = soup.find("div", id="historical-desc")
code
if not container:
return None
content_source = container.find(["h2", "h3"]) or container
return content_source.get_text(strip=True, separator=" ")
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
The funny looking “’” isn’t a BeautifulSoup problem at all – it almost always means
the page itself is encoded as UTF-8 (good)
you fetched the raw bytes correctly (good)
but those bytes were decoded as Latin-1/Windows-1252 instead of UTF-8 (bad)
When the three UTF-8 bytes that represent the curly apostrophe (E2 80 99) are treated as Latin-1 you get the three characters “’”.
So the real fix is: make sure the HTML is decoded with UTF-8 before you hand it to BeautifulSoup.
Below are two complementary ways to do that.
Solution A – decode correctly when you download
python
import requests
from bs4 import BeautifulSoup
def fetch_html(url: str) -> str:
resp = requests.get(url, timeout=30)
# Requests sometimes guesses the wrong encoding.
# Force the right one (most sites are UTF-8 nowadays).
resp.encoding = "utf-8"
return resp.text # <- now a proper str
def parse_summary_paragraph(html_content: str) -> str | None:
soup = BeautifulSoup(html_content, "html.parser")
container = soup.find("div", id="historical-desc")
if not container:
return None
content_source = container.find(["h2", "h3"]) or container
# Convert curly quotes to plain ASCII while we’re at it
text = content_source.get_text(" ", strip=True)
return text.replace("’", "'")
Nothing else has to change—once the string is decoded as UTF-8,
“South Africa’s” shows up exactly as you want.
Solution B – fix it inside your parser (fallback)
Sometimes you can’t control how the HTML string reaches you.
You can still “un-mojibake” it inside the parser:
python
from bs4 import BeautifulSoup
import unicodedata
def _fix_possible_mojibake(text: str) -> str:
"""
Try to undo the common UTF-8-decoded-as-Latin1 error.
If the attempt fails we just return the original text.
"""
try:
return text.encode("latin1").decode("utf-8")
except UnicodeEncodeError:
# `text` already contains characters that cannot be encoded as Latin-1,
# so it probably wasn’t mojibaked in the first place.
return text
def parse_summary_paragraph(html_content: str) -> str | None:
html_content = _fix_possible_mojibake(html_content)
soup = BeautifulSoup(html_content, "html.parser")
container = soup.find("div", id="historical-desc")
if not container:
return None
content_source = container.find(["h2", "h3"]) or container
text = content_source.get_text(" ", strip=True)
# Normalise fancy punctuation → ASCII equivalents
text = (text.replace("’", "'")
.replace("‘", "'")
.replace("“", '"')
.replace("”", '"'))
text = unicodedata.normalize("NFKC", text)
return text