Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/335.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何使用BeautifulSoup将UTF-8编码的HTML正确解析为Unicode字符串?_Python_Unicode_Utf 8_Beautifulsoup_Urllib2 - Fatal编程技术网

Python 如何使用BeautifulSoup将UTF-8编码的HTML正确解析为Unicode字符串?

Python 如何使用BeautifulSoup将UTF-8编码的HTML正确解析为Unicode字符串?,python,unicode,utf-8,beautifulsoup,urllib2,Python,Unicode,Utf 8,Beautifulsoup,Urllib2,我正在运行一个Python程序,它获取一个UTF-8编码的网页,并使用BeautifulSoup从HTML中提取一些文本 但是,当我将此文本写入文件(或在控制台上打印)时,它会以意外的编码写入 示例程序: 导入urllib2 从BeautifulSoup导入BeautifulSoup #获取URL url='1〕http://www.voxnow.de/' request=urlib2.request(url) 请求。添加_头('Accept-Encoding','utf-8') #响应具有UT

我正在运行一个Python程序,它获取一个UTF-8编码的网页,并使用BeautifulSoup从HTML中提取一些文本

但是,当我将此文本写入文件(或在控制台上打印)时,它会以意外的编码写入

示例程序:

导入urllib2
从BeautifulSoup导入BeautifulSoup
#获取URL
url='1〕http://www.voxnow.de/'
request=urlib2.request(url)
请求。添加_头('Accept-Encoding','utf-8')
#响应具有UTF-8字符集头,
#以及UTF-8编码的HTML正文
response=urllib2.urlopen(请求)
#使用BeautifulSoup进行解析
汤=美汤(响应)
#使用umlauts的打印标题属性(例如können)
打印报告(soup.find('div',id='navbutton\u account')['title']))
运行此命令将得到以下结果:

# u'Hier k\u0102\u015bnnen Sie sich kostenlos registrieren und / oder einloggen!'
但是我希望Python Unicode字符串在单词
können
中呈现为:

我尝试将'fromEncoding'参数传递给BeautifulSoup,并尝试
read()
decode()
响应对象,但它要么没有任何区别,要么抛出错误

使用命令
curl www.voxnow.de | hextump-C
,我可以看到网页确实对
字符进行了UTF-8编码(即它包含
0xc3 0xb6
):

      20 74 69 74 6c 65 3d 22  48 69 65 72 20 6b c3 b6  | title="Hier k..|
      6e 6e 65 6e 20 53 69 65  20 73 69 63 68 20 6b 6f  |nnen Sie sich ko|
      73 74 65 6e 6c 6f 73 20  72 65 67 69 73 74 72 69  |stenlos registri|

我已经超出了Python能力的极限,因此我不知道如何进一步调试它。有什么建议吗?

将结果编码到
utf-8
似乎对我很有用:

print (soup.find('div', id='navbutton_account')['title']).encode('utf-8')
它产生:

Hier können Sie sich kostenlos registrieren und / oder einloggen!

正如上文所指出的,我这里的问题本质上是一个重复的问题

HTML内容报告自己是UTF-8编码的,大多数情况下是,除了一两个非法的无效UTF-8字符

这显然混淆了BeautifulSoup正在使用的编码,以及在将内容传递给BeautifulSoup时尝试首次解码为UTF-8 这:

我会得到一个错误:

UnicodeDecodeError: 'utf8' codec can't decode bytes in position 186812-186813: 
                    invalid continuation byte
更仔细地观察输出,有一个字符
Ü
的实例被错误地编码为无效字节序列
0xe3 0x9c
,而不是正确的

正如当前关于该问题的建议,解析时可以删除无效的UTF-8字符,以便只将有效数据传递给BeautifulSoup:

soup = BeautifulSoup(response.read().decode('utf-8', 'ignore'))

奇怪。。正如
\u0102\u015b
“Ăś”
。这不是重复了这个问题吗:@我想我看到了那个问题,但我没有得到同样的结果。但我会再次检查,谢谢。我会倾向于这个答案,并使用请求库和原始内容。。我在几台机器上进行了尝试(使用Python 2.7.3);该代码为我提供了四个字节,而不是
ö
字符的两个字节:
c482c59b
UnicodeDecodeError: 'utf8' codec can't decode bytes in position 186812-186813: 
                    invalid continuation byte
soup = BeautifulSoup(response.read().decode('utf-8', 'ignore'))