BeautifulSoup Prettify fails on copyright symbol
Asked Answered
R

4

10

I am getting a Unicode error: UnicodeEncodeError: 'charmap' codec can't encode character u'\xa9' in position 822: character maps to <undefined>

This appears to be a standard copyright symbol, and in the HTML is &copy. I have not been able to find a way past this. I even tried a custom function to replace copy with a space but that also failed with the same error.

import sys
import pprint
import mechanize
import cookielib
from bs4 import BeautifulSoup
import html2text
import lxml

def MakePretty():

def ChangeCopy(S):
    return S.replace(chr(169)," ")
br = mechanize.Browser()

# Cookie Jar
cj = cookielib.LWPCookieJar()
br.set_cookiejar(cj)

# Browser options
br.set_handle_equiv(True)
#br.set_handle_gzip(True)
br.set_handle_redirect(True)
br.set_handle_referer(True)
br.set_handle_robots(False)

# Follows refresh 0 but not hangs on refresh > 0
br.set_handle_refresh(mechanize._http.HTTPRefreshProcessor(), max_time=1)

# User-Agent (this is cheating, ok?)
br.addheaders = [('User-agent', 'Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.0.1) Gecko/2008071615 Fedora/3.0.1-1.fc9 Firefox/3.0.1')]

# The site we will navigate into, handling its session
# Open the site
br.open('http://www.thesitewizard.com/faqs/copyright-symbol.shtml')
html = br.response().read()
soup = BeautifulSoup(html)
print soup.prettify()

if __name__ == '__main__':
    MakePretty()

How do I get prettify past the copyright symbol? I have searched all over the web for a solution to no avail (or I might not understand as I am fairly new to Python and scraping).

Thanks for your help.

Rogatory answered 12/7, 2012 at 17:9 Comment(2)
What encoding does the page use?Squiffy
Probably caused by incorrect console/environment settings. See: https://mcmap.net/q/1161286/-stuck-with-encodings-in-python-with-beautifulsoupUncomfortable
H
27

I had the same problem. This may work for you:

print soup.prettify().encode('UTF-8')

Hype answered 23/11, 2012 at 7:32 Comment(4)
@sliders_alpha: I believe it is because (in the case of the OP), BeautifuSoup guessed the encoding of the page incorrectly. See wiki.python.org/moin/UnicodeEncodeErrorHype
Had same problem with a Korean page. I wonder why beautiful soup doesn't use utf8 by default... The charset of the page is defined as utf8 too...Levalloisian
5 years later this solution got me unstuck too. :)Isahella
without "encode('UTF-8')" did worked for me.. thumps up dearThough
B
1

The page http://www.thesitewizard.com/faqs/copyright-symbol.shtml is sent without specifying character encoding. The page itself specifies the encoding as ISO-8859-1 in a meta tag, but only after the occurrence of the “©” character. So clients have to make a guess, and the guess may be wrong. If the client guesses UTF-8, then it will see the bit A9, which is a data error in UTF-8 data.

So it seems that you need to set the encoding (to ISO-8859-1, or more safely to windows-1252) when reading the data. This is of course an ad hoc solution only; it makes no sense to fix the encoding in general.

Beeeater answered 12/7, 2012 at 17:53 Comment(1)
Yes, we’re talking about “©”; I’ve fixed this in my answer.Beeeater
H
0

You're using chr(), which is wrong here because it expects ASCII and that goes only as far as 127/0x7F only (despite to popular folklore, ASCII is 7-bit only). 0xA9 / © is Unicode, so you should use unichr(169) instead.

Hanhhank answered 12/7, 2012 at 17:57 Comment(0)
R
0

Just changing to unichr in the formatter function didn't work. Ended up using decode(formatter=blah) which did return unformatted html without the copyright symbol. Saved that html and fed that into prettify which did the trick.

Rogatory answered 13/7, 2012 at 13:58 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.