Python beauthulsoup+MySq编码错误

2024-09-28 21:24:10 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在使用BeautifulSoup python库。 我使用urllib2库从页面下载HTML代码,然后用beauthoulsoup对其进行了解析。 我想把一些HTML内容保存到MySql表中,但是在编码方面遇到了一些问题。MySql表使用'utf-8'字符集编码。在

一些例子:

当我下载HTML代码并用beauthoulsoup解析它时,我得到了如下内容:

"Ver las \xc3\xbaltimas noticias. Ent\xc3\xa9rate de las noticias de \xc3\xbaltima hora con la mejor cobertura con fotos y videos"

正确的文本应该是:

"Ver las últimas noticias. Entérate de las noticias de última hora con la mejor cobertura con fotos y videos"

我曾尝试过用多个字符集对文本进行编码和解码,但当我将其插入MySql时,我会发现如下内容:

"Ver las últimas noticias y todos los titulares de hoy en Yahoo! Noticias Argentina. Entérate de las noticias de última hora con la mejor cobertura con fotos y videos"

我的编码有问题,但我不知道怎么解决。在

有什么建议吗?在


Tags: 内容编码htmlmysqlcoberturadeconla
2条回答

您有正确的UTF-8数据来自beauthoulsoup,但它是以普通字符串类型存储的,而不是python的本机unicode字符串类型。我想这就是你需要做的:

codecs.decode(your_string, 'utf-8')

然后字符串应该是正确的数据类型和编码以发送到mysql。在

例如:

^{pr2}$

beauthulsoup以unicode字符串的形式返回所有数据。首先三次检查unicode字符串是否正确。如果没有,则输入数据的编码存在一些问题。在

相关问题 更多 >