【问题标题】:hi § symbol unrecognized嗨§符号无法识别
【发布时间】:2016-11-28 00:29:35
【问题描述】:

早上好。 我正在努力做到这一点,而不是离开我。

你能帮帮我吗?

非常感谢

 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)

【问题讨论】:

  • h3的文字是什么?

标签: python beautifulsoup symbols


【解决方案1】:

Define the coding 并使用 unicode 字符串

# -*- 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

【讨论】:

  • tkns 我只需要 "u" Tks。
【解决方案2】:

我会向你解释清楚是什么问题:

默认情况下,Python 不识别“à”或“ò”等特定字符。要让 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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-13
    • 2023-03-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-17
    相关资源
    最近更新 更多