2016-07-24 137 views
0

早上好。 我試圖做到這一點,而不是離開我。hi§符號無法識別

你能幫我嗎?

非常感謝你

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) 
+0

'h3'的文字是什麼? –

回答

3

Define the codingunicode字符串操作:

# -*- coding: utf-8 -*- 
from bs4 import BeautifulSoup 

html_page = u"<h3>§ title here</h3>" 

soup = BeautifulSoup(html_page, "html.parser") 

titulo = soup.find('h3').get_text() 
titulo = titulo.replace(u'§', '') 
print(titulo) 

打印title here

+0

tkns我只需要「u」Tks。 –

0

我會解釋清楚你有什麼問題:

默認情況下,Python不承認,如「A」或「O」的特定字符。爲了使Python認識那些你必須把你的腳本的頂部特徵:

# -*- coding: utf-8 -*- 

此代碼將使Python的認識到,在默認情況下不被識別的特定字符。 另一種使用編碼的方法是使用「sys」庫:

# sys.setdefaultencoding() does not exist, here! 
import sys 
reload(sys) #This reloads the sys module 
sys.setdefaultencoding('UTF8') #Here you choose the encoding