tldr
tldr

Reputation: 12112

Character encoding error: UnicodeEncodeError: 'charmap' codec can't encode character X in position Y: character maps to <undefined>

I'm trying to scrape yahoo finance web pages to get stock price data with Python 3.3, httplib2, and beautifulsoup4. Here is the code:

def getData (symbol = 'GOOG', period = 'm'):
    baseUrl = 'http://finance.yahoo.com/q/hp?s='
    url = baseUrl + symbol + '&g=' + period
    h = httplib2.Http('.cache')
    response, content = h.request(url)
    soup = BeautifulSoup(content)
    print(soup.prettify()) 

getData()

I get the following error trace:

File "/Library/Frameworks/Python.framework/Versions/3.3/lib/python3.3/encodings/mac_roman.py", line 19, in encode
return codecs.charmap_encode(input,self.errors,encoding_table)[0]
UnicodeEncodeError: 'charmap' codec can't encode character '\xd7' in position 11875: character maps to <undefined>

I'm new to python and the libraries and would greatly appreciate your help!

Upvotes: 4

Views: 3034

Answers (1)

Eish
Eish

Reputation: 1081

This is due to the encoding of your console.

Depending on which console you're working in (Windows, Mac, Linux) the console is trying to display characters it doesn't recognize and therefore can't print to screen.

You could try converting the output string into the encoding of your console.

I found an easy way was to just convert your data into a string and it prints just fine.

Upvotes: 1

Related Questions