Tag: beautifulsoup

UnicodeEncodeError:'ascii'编解码器不能编码字符u'\ xa0'在位置20:序号不在范围内(128)

我在处理来自不同网页(不同网站)的文本中的Unicode字符时遇到了问题。 我正在使用BeautifulSoup。 问题是错误不总是可重现的; 它有时可以和一些页面一起工作,有时它通过抛出一个UnicodeEncodeError 。 我已经尝试了所有我能想到的事情,但是我还没有发现任何能够一致工作的东西,而没有抛出某种与Unicode相关的错误。 下面显示了导致问题的代码段之一: agent_telno = agent.find('div', 'agent_contact_number') agent_telno = '' if agent_telno is None else agent_telno.contents[0] p.agent_info = str(agent_contact + ' ' + agent_telno).strip() 当上面的代码片段运行时,这是在一些string上产生的堆栈跟踪: Traceback (most recent call last): File "foobar.py", line 792, in <module> p.agent_info = str(agent_contact + ' ' + agent_telno).strip() UnicodeEncodeError: 'ascii' codec can't encode character u'\xa0' […]