我收到Unicode错误:UnicodeEncodeError: 'charmap' codec can't encode character u'\xa9' in position 822: character maps to <undefined>
这似乎是一个标准的版权符号,并且在HTML中是&copy。我还没有找到解决这个问题的办法。我什至尝试使用自定义函数将副本替换为空格,但同样失败并失败。

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()

我如何美化过去的版权符号?我在网上搜索了无济于事的解决方案(或者因为我对Python和抓取还很陌生,所以我可能不明白)。

谢谢你的帮助。

最佳答案

我有同样的问题。这可能对您有用:
print soup.prettify().encode('UTF-8')

关于python - BeautifulSoup Prettify在版权符号上失败,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/11457009/

10-10 16:42