如果我运行此代码:的Python + BeautifulSoup:编码错误
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,打印已导致错误 – Maviles