Python出现UnicodeEncodeError: 'gbk' codec can't encode character '\u22ef' in position 17: illegal multibyte sequence的错误
起因:在Python3.6.4中能够运行,Python3.5.1 中出现该错误
解决办法:加encoding = 'utf-8'
原代码:(部分代码)
-
- comment = re.findall(r'(?<=<span class="inq">).*?(?=</span>)', str(info))
- if comment == []:
- comment = '此片没有评价'
- else:
- comment = comment[0]
-
- score = re.findall(r'<span class="rating_num" property="v:average">(.*?)</span>', str(info))[0]
- time.sleep(1)
-
- f = open(r"C:\Users\User\AppData\Local\Programs\Python\Python35\新建文件夹\douban.txt", "a",encoding = 'utf-8')
修改后:
-
- comment = re.findall(r'(?<=<span class="inq">).*?(?=</span>)', str(info))
- if comment == []:
- comment = '此片没有评价'
- else:
- comment = comment[0]
-
- score = re.findall(r'<span class="rating_num" property="v:average">(.*?)</span>', str(info))[0]
- time.sleep(1)
-
- f = open(r"C:\Users\User\AppData\Local\Programs\Python\Python35\新建文件夹\douban.txt", "a",encoding = 'utf-8')