解决utf8' codec can't decode byte 0xe5 in position 0: unexpected end of data

使用unicode对象的话,除了这样使用u标记,还可以使用unicode类以及字符串的encode和decode方法。

unicode类的构造函数接受一个字符串参数和一个编码参数,将字符串封装为一个unicode,比如在这里,由于我们用的是utf-8编码,所以unicode中的编码参数使用’utf-8′将字符封装为unicode对象,然后正确输出到控制台:

i = '多大2%2大沙地'
 driver.find_element_by_id('search').send_keys(i)
#会报错icodeDecodeError: 'utf8' codec can't decode byte 0xe5 in position 0: unexpected end of data

i = '多大2%2大沙地'
a = unicode(i, 'utf-8')
driver.find_element_by_id('search').send_keys(a)
#这样就可以了

 

posted @ 2019-03-27 11:21  被窝里的ymk  阅读(5379)  评论(0编辑  收藏  举报