0
votes

Im getting error:

File "C:\Python34\lib\encodings\cp1252.py", line 19, in encode return codecs.charmap_encode(input,self.errors,encoding_table)[0] UnicodeEncodeError: 'charmap' codec can't encode character '\u0106' in position 73: character maps to

This is my code:

import requests
from bs4 import BeautifulSoup

url = 'http://www.privredni-imenik.com/firma/68225-a_expo'
r = requests.get(url)

soup = BeautifulSoup(r.content, "html.parser")

g_data = soup.find_all("div", {"class":"podaci"})
print(g_data)

How can i encode data in utf-8. i have tried solutions from other topics, but none of them work for me.

1
do you have # -*- coding: utf-8 -*- at the begging of your file? - Dušan Maďar
Didnt have, i have just added it and its the same. - RhymeGuy
try g_data.decode('utf_8') - Sam cd
AttributeError: 'ResultSet' object has no attribute 'decode' - RhymeGuy

1 Answers

1
votes
import requests
from bs4 import BeautifulSoup

url = 'http://www.privredni-imenik.com/firma/68225-a_expo'
r = requests.get(url)

soup = BeautifulSoup(r.content, "html.parser")

g_data = soup.find_all("div", {"class":"podaci"})
for i in g_data:
    some = i.text.encode('utf-8', 'replace')
    print (some)

It works, im receiving data but with weird characters.. Well thats question for another topic :)