Python 嗨§;无法识别的符号
早上好。 我正在努力做到这一点,而不是离开我 你能帮我吗 多谢各位Python 嗨§;无法识别的符号,python,beautifulsoup,symbols,Python,Beautifulsoup,Symbols,早上好。 我正在努力做到这一点,而不是离开我 你能帮我吗 多谢各位 soup = BeautifulSoup(html_page) titulo=soup.find('h3').get_text() titulo=titulo.replace('§','') titulo=titulo.replace('§','') UnicodeDecodeError: 'ascii' codec can't decode byte 0xc2 in position
soup = BeautifulSoup(html_page)
titulo=soup.find('h3').get_text()
titulo=titulo.replace('§','')
titulo=titulo.replace('§','')
UnicodeDecodeError: 'ascii' codec can't decode byte 0xc2 in position 0: ordinal not in range(128)
并使用unicode字符串进行操作:
#-*-编码:utf-8-*-
从bs4导入BeautifulSoup
html_page=u“§此处标题”
soup=BeautifulSoup(html_页面,“html.parser”)
titulo=soup.find('h3')。get_text()
titulo=titulo.替换(u'§','')
印刷品(提图洛)
在此处打印标题
并使用unicode字符串进行操作:
#-*-编码:utf-8-*-
从bs4导入BeautifulSoup
html_page=u“§此处标题”
soup=BeautifulSoup(html_页面,“html.parser”)
titulo=soup.find('h3')。get_text()
titulo=titulo.替换(u'§','')
印刷品(提图洛)
在此处打印
标题
我将向您解释清楚问题所在:
默认情况下,Python不识别诸如“a”或“ò”之类的特定字符。要使Python识别这些字符,必须将其放在脚本顶部:
# -*- coding: utf-8 -*-
这段代码使Python能够识别默认情况下无法识别的特定字符。
使用编码的另一种方法是使用“sys”库:
我会给你解释清楚问题是什么: 默认情况下,Python不识别诸如“a”或“ò”之类的特定字符。要使Python识别这些字符,必须将其放在脚本顶部:
# -*- coding: utf-8 -*-
这段代码使Python能够识别默认情况下无法识别的特定字符。
使用编码的另一种方法是使用“sys”库:
h3的文本是什么?h3的文本是什么?tkns我只需要“u”Tks。tkns我只需要“u”Tks。
# sys.setdefaultencoding() does not exist, here!
import sys
reload(sys) #This reloads the sys module
sys.setdefaultencoding('UTF8') #Here you choose the encoding