scrape with correct character encoding (python requests + beautifulsoup)

Viewed 9715

I have an issue parsing this website: http://fm4-archiv.at/files.php?cat=106

It contains special characters such as umlauts. See here:enter image description here

My chrome browser displays the umlauts properly as you can see in the screenshot above. However on other pages (e.g.: http://fm4-archiv.at/files.php?cat=105) the umlauts are not displayed properly, as can be seen in the screenshot below: enter image description here

The meta HTML tag defines the following charset on the pages:

<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"/>

I use the python requests package to get the HTML and then use Beautifulsoup to scrape the desired data. My code is as follows:

r = requests.get(URL)
soup = BeautifulSoup(r.content,"lxml")

If I print the encoding (print(r.encoding) the result is UTF-8. If I manually change the encoding to ISO-8859-1 or cp1252 by calling r.encoding = ISO-8859-1 nothing changes when I output the data on the console. This is also my main issue.

r = requests.get(URL)
r.encoding = 'ISO-8859-1'
soup = BeautifulSoup(r.content,"lxml")

still results in the following string shown on the console output in my python IDE:

Der Wildlöwenpfleger

instead it should be

Der Wildlöwenpfleger

How can I change my code to parse the umlauts properly?

2 Answers
Related