【发布时间】:2016-12-28 06:59:37
【问题描述】:
如果我运行这段代码:
for link in soup.findAll('a'):
href = link.get('href')
href = str(href)
最后一行出现以下错误
href = str(href)
UnicodeEncodeError: 'ascii' codec can't encode character u'\u2018' in position 68: ordinal not in range(128)
当我尝试对变量进行编码时,如下图:
for link in soup.findAll('a'):
href = link.get('href')
href = href.encode('utf-8')
href = str(href)
我收到以下错误:
href = href.encode('utf-8')
AttributeError: 'NoneType' object has no attribute 'encode'
我在这里和其他地方查看了多个帖子,但没有一个提供合适的解决方案。我对python相当陌生。请帮忙。
【问题讨论】:
-
您可以使用 try/catch 打印导致错误的值
标签: python python-2.7 encoding beautifulsoup