作者:朱美萍客户 | 来源:互联网 | 2023-10-16 13:39
IamgettingaUnicodeerror:UnicodeEncodeError:charmapcodeccantencodecharacteru\xa9in
I am getting a Unicode error: UnicodeEncodeError: 'charmap' codec can't encode character u'\xa9' in position 822: character maps to
我得到一个Unicode错误:UnicodeEncodeError:“charmap”编解码器不能在位置822对字符u'\xa9'进行编码:字符映射到
This appears to be a standard copyright symbol, and in the HTML is ©. 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.
这似乎是一个标准的版权符号,在HTML是©。我一直没能找到一个办法过去。我甚至尝试了一个自定义函数来替换一个空格,但同样的错误也让我失败了。
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).
我怎样才能在经过版权标志后变得更漂亮呢?我已经在web上搜索了所有的解决方案,但是没有任何用处(或者我可能不理解,因为我对Python和抓取非常陌生)。
Thanks for your help.
谢谢你的帮助。
4 个解决方案