【问题标题】:Yet another encoding issue with accented characters (scraping a Website with Python and BeautifulSoup)重音字符的另一个编码问题(使用 Python 和 BeautifulSoup 抓取网站)
【发布时间】:2017-02-01 23:44:58
【问题描述】:

(前言:我知道,这个问题已经讲了一百遍了,但我还是不明白)

我正在尝试加载一个 html 页面并输出文本,即使我正确获取了网页,BeautifulSoup 以某种方式破坏了不属于前 127 个 ASCII 字符的重音字符的编码:

# -*- coding: utf-8 -*-
import sys
from urllib import urlencode
from urlparse import parse_qsl
import re
import urlparse
import json
import urllib
from bs4 import BeautifulSoup

url = "http://www.rtve.es/alacarta/interno/contenttable.shtml?ctx=29010&locale=es&module=&orderCriteria=DESC&pageSize=15&mode=TEXT&seasonFilter=40015"
html=urllib2.urlopen(url).read()
soup = BeautifulSoup(html)
div = soup.find_all("span", class_="detalle")
capitulo_detalle = div[0].text   (doesn't work, capitulo_detalle is type str with utf-8, div[0].tex is type unicode)

div[0].text 的输出应该类似于:

Sátur se dirige al sur en busca de Estuarda y Gabi, pero un compañero de viaje inesperado hará que cambie德伦博。 Los hombres de Juan siguen presos。 El enemigo comienza a realizar ejecuciones。 Águila Roja tiene...

但我得到的结果是:

u'S\xe1tur se dirige al sur en busca de Estuarda y Gabi, pero un compa\xf1ero de viaje inesperado har\xe1 que cambie de rumbo。胡安·西根·普雷索斯 . El enemigo comienza a realizar ejecuciones。 \xc1guila Roja tiene...'

--> 要获得“正确”的字符,我必须进行哪些更改?

我知道它一定是这些问题的重复,但答案似乎在这里不起作用: Python and BeautifulSoup encoding issues How to correctly parse UTF-8 encoded HTML to Unicode strings with BeautifulSoup?

我还阅读了有关 unicode、utf-8、ascii 的典型文档,例如https://docs.python.org/3/howto/unicode.html,显然没有成功……

【问题讨论】:

  • 如何运行这段代码?在 Python Shell 或 python script.py 中?你如何得到这个文本?您是否使用了print div[0].text 或 Python Shell 自动为您打印了这个?您有正确的文本,但 Python Shell 使用 print repr( div[0].text ) 显示对调试有用的文本。所以试试print repr(div[0].text)print div[0].text 你会看到不同的文字。
  • 我使用的是 Python 2.7.13,示例可以在 Shell 中运行,也可以作为脚本运行,没关系。是的,“打印”显示正确的输出,但我需要变量中的文本。
  • 你已经有了正确的字符。文字u'S\xe1tur se dirige...' 正好代表文字Sátur se dirige...。如果您 print() 它,您将看到原始字符(假设您的控制台可以打印它们,如果是 Windows 则可能不会)。
  • @bobince:是的,但我使用的是 Python 2.7.13 和 utf-8。如果我将 div[0].text(它是 unicode)分配给一个普通的字符串变量(它是 utf-8),我就会遇到麻烦。
  • 仅使用 unicode 就没有问题了 - 所以将所有字符串转换为 unicode。这是解决方案。

标签: python unicode utf-8 character-encoding beautifulsoup


【解决方案1】:

我相信我终于明白了......

>>> div = soup.find("span", class_="detalle")
>>> div.text
u'S\xe1tur se dirige al sur en busca de Estuarda y Gabi, pero

---> 这是 unicode,\xe1 是 'á' 的'code' (http://www.utf8-chartable.de/unicode-utf8-table.pl?start=4096&number=128&names=-&utf8=string-literal)

>>> print(div.text)
Sátur se dirige al sur en busca de Estuarda y Gabi, pero

---> 'print' 正确评估 unicode 代码点

>>> div.text.encode('utf-8')
'S\xc3\xa1tur se dirige al sur en busca de Estuarda y Gabi, pero

---> 根据上面引用的 url 上给出的表格,Unicode 被编码为 utf-8。我不明白为什么输出显示为 \xc3\xa1 而不是'á'。

>>> print div.text.encode('utf-8')
Sátur se dirige al sur en busca de Estuarda y Gabi, pero

--->我不明白为什么 print 现在将它评估为一个奇怪的符号....

>>> blurr = div.text.encode('cp850')
>>> blurr
'S\xa0tur se dirige al sur en busca de Estuarda y Gabi, pero
>>> type(blurr)
<type 'str'>

---> Unicode 编码为代码页 850,在 Windows 下的 python-shell 中使用

>>> print(blurr)
Sátur se dirige al sur en busca de Estuarda y Gabi, pero

--->终于对了!!!

在 Kodi 中,我可以使用 utf-8 表示,例如字符 'á' 在变量中保存为 \xc3\xa1,但是当变量的内容显示为例如“xbmcgui.Dialog().ok(addonname, blurr) 时,它会正确显示在屏幕上,并带有'á'......

Und sowas soll man wissen......

【讨论】:

    【解决方案2】:
    import requests
    from bs4 import BeautifulSoup
    
    url = "http://www.rtve.es/alacarta/interno/contenttable.shtml?ctx=29010&locale=es&module=&orderCriteria=DESC&pageSize=15&mode=TEXT&seasonFilter=40015"
    html=requests.get(url)
    soup = BeautifulSoup(html.text, 'lxml')
    div = soup.find("span", class_="detalle")
    capitulo_detalle = div.text 
    

    出来:

    'Sátur se dirige al sur en busca de Estuarda y Gabi, pero un compañero de viaje inesperado hará que cambie de rumbo. Los hombres de Juan siguen presos. El enemigo comienza a realizar ejecuciones. Águila Roja tiene...'
    

    使用requestspython3,问题永远不会出现

    【讨论】:

    • 我还尝试了带有“请求”的示例,这并没有改变任何东西。好吧,python3 先验地处理 unicode,但现在我从 python 2.7.13 开始,我不想更改整个代码,寻找不一致之处。此外,我不知道 Kodi 是否支持 python3。
    猜你喜欢
    • 1970-01-01
    • 2011-09-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-09-26
    • 1970-01-01
    相关资源
    最近更新 更多