相关疑难解决方法(0)

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

我正在运行一个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)

python unicode urllib2 beautifulsoup utf-8

25
推荐指数
1
解决办法
7万
查看次数

Python正确的网站编码(美丽的汤)

我正在尝试加载一个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

python encoding beautifulsoup utf-8 mojibake

9
推荐指数
2
解决办法
1万
查看次数

标签 统计

beautifulsoup ×2

python ×2

utf-8 ×2

encoding ×1

mojibake ×1

unicode ×1

urllib2 ×1