我正在运行一个Python程序,它获取一个UTF-8编码的网页,我使用BeautifulSoup从HTML中提取一些文本.
但是,当我将此文本写入文件(或在控制台上打印)时,它将以意外编码形式写入.
示例程序:
import urllib2
from BeautifulSoup import BeautifulSoup
# Fetch URL
url = 'http://www.voxnow.de/'
request = urllib2.Request(url)
request.add_header('Accept-Encoding', 'utf-8')
# Response has UTF-8 charset header,
# and HTML body which is UTF-8 encoded
response = urllib2.urlopen(request)
# Parse with BeautifulSoup
soup = BeautifulSoup(response)
# Print title attribute of a <div> which uses umlauts (e.g. können)
print repr(soup.find('div', id='navbutton_account')['title'])
Run Code Online (Sandbox Code Playgroud)
运行它会得到结果:
# u'Hier k\u0102\u015bnnen Sie sich kostenlos registrieren und / oder einloggen!'
Run Code Online (Sandbox Code Playgroud)
但我希望Python Unicode字符串ö在单词中呈现können为\xf6:
# …Run Code Online (Sandbox Code Playgroud) 我正在尝试加载一个html页面并输出文本,即使我正确地获取网页,BeautifulSoup会以某种方式破坏编码.
资源:
# -*- coding: utf-8 -*-
import requests
from BeautifulSoup import BeautifulSoup
url = "http://www.columbia.edu/~fdc/utf8/"
r = requests.get(url)
encodedText = r.text.encode("utf-8")
soup = BeautifulSoup(encodedText)
text = str(soup.findAll(text=True))
print text.decode("utf-8")
Run Code Online (Sandbox Code Playgroud)
摘录输出:
...Odenw\xc3\xa4lderisch...
Run Code Online (Sandbox Code Playgroud)
这应该是Odenwälderisch