美丽汤中的 UnicodeEncodeError (python 2.7.1)

发布于 2024-12-28 19:06:24 字数 2149 浏览 3 评论 0原文

我在这里在 python 2.7.1 上使用 Beautiful Soup 3.2 。

我最近一直在尝试做一些简单的事情,但它 似乎相当棘手:

我执行以下操作:

temp=BeautifulSoup(urllib2.urlopen(urlList[1], None,15))

但是,我收到错误:

File "/home/foo/k/kat/BeautifulSoup.py", line 1519, in __init__
BeautifulStoneSoup.__init__(self, *args, **kwargs)
File "/home/foo/k/kat/BeautifulSoup.py", line 1144, in __init__
self._feed(isHTML=isHTML)
File "/home/foo/k/kat/BeautifulSoup.py", line 1186, in _feed
SGMLParser.feed(self, markup)
File "/usr/lib/python2.7/sgmllib.py", line 104, in feed
self.goahead(0)
File "/usr/lib/python2.7/sgmllib.py", line 143, in goahead
k = self.parse_endtag(i)
File "/usr/lib/python2.7/sgmllib.py", line 320, in parse_endtag
self.finish_endtag(tag)
File "/usr/lib/python2.7/sgmllib.py", line 358, in finish_endtag
method = getattr(self, 'end_' + tag)
UnicodeEncodeError: 'ascii' codec can't encode character u'\xfa' in
position 4: ordinal not in range(128)

如果我再次运行相同的循环,有时,我还会得到:

File "/home/foo/k/kat/BeautifulSoup.py", line 1519, in __init__
BeautifulStoneSoup.__init__(self, *args, **kwargs)
File "/home/foo/k/kat/BeautifulSoup.py", line 1144, in
__init__
self._feed(isHTML=isHTML)
File "/home/foo/k/kat/BeautifulSoup.py", line 1186, in _feed
SGMLParser.feed(self, markup)
File "/usr/lib/python2.7/sgmllib.py", line 104, in feed
self.goahead(0)
File "/usr/lib/python2.7/sgmllib.py", line 143, in goahead
k = self.parse_endtag(i)
File "/usr/lib/python2.7/sgmllib.py", line 320, in parse_endtag
self.finish_endtag(tag)
File "/usr/lib/python2.7/sgmllib.py", line 358, in finish_endtag
method = getattr(self, 'end_' + tag)
UnicodeEncodeError: 'ascii' codec can't encode characters in position
4-5: ordinal not in range(128)

如何避免此错误?显然 sgmllib.py 有问题。

我尝试了 SOF 的一些解决方案:

*]尝试了 soup = BeautifulSoup(page, fromEncoding=) 结果:没有工作,同样的错误。

*] 尝试将 sgmllib.py 从 2.7.2 版本升级到 2.7.1 版本 结果:没有工作,同样的错误。

*]尝试 html = BeautifulSoup(page.encode('utf-8')) 结果:没有工作,同样的错误。

我将不胜感激任何有关如何解决此编码错误的建议。

I am using Beautiful Soup 3.2 on python 2.7.1 here.

I have recently been trying to get something simple to work, but it
seems rather tricky:

I do the following:

temp=BeautifulSoup(urllib2.urlopen(urlList[1], None,15))

However, I get the error:

File "/home/foo/k/kat/BeautifulSoup.py", line 1519, in __init__
BeautifulStoneSoup.__init__(self, *args, **kwargs)
File "/home/foo/k/kat/BeautifulSoup.py", line 1144, in __init__
self._feed(isHTML=isHTML)
File "/home/foo/k/kat/BeautifulSoup.py", line 1186, in _feed
SGMLParser.feed(self, markup)
File "/usr/lib/python2.7/sgmllib.py", line 104, in feed
self.goahead(0)
File "/usr/lib/python2.7/sgmllib.py", line 143, in goahead
k = self.parse_endtag(i)
File "/usr/lib/python2.7/sgmllib.py", line 320, in parse_endtag
self.finish_endtag(tag)
File "/usr/lib/python2.7/sgmllib.py", line 358, in finish_endtag
method = getattr(self, 'end_' + tag)
UnicodeEncodeError: 'ascii' codec can't encode character u'\xfa' in
position 4: ordinal not in range(128)

If I run the same loop another time, sometimes, I also get:

File "/home/foo/k/kat/BeautifulSoup.py", line 1519, in __init__
BeautifulStoneSoup.__init__(self, *args, **kwargs)
File "/home/foo/k/kat/BeautifulSoup.py", line 1144, in
__init__
self._feed(isHTML=isHTML)
File "/home/foo/k/kat/BeautifulSoup.py", line 1186, in _feed
SGMLParser.feed(self, markup)
File "/usr/lib/python2.7/sgmllib.py", line 104, in feed
self.goahead(0)
File "/usr/lib/python2.7/sgmllib.py", line 143, in goahead
k = self.parse_endtag(i)
File "/usr/lib/python2.7/sgmllib.py", line 320, in parse_endtag
self.finish_endtag(tag)
File "/usr/lib/python2.7/sgmllib.py", line 358, in finish_endtag
method = getattr(self, 'end_' + tag)
UnicodeEncodeError: 'ascii' codec can't encode characters in position
4-5: ordinal not in range(128)

How do I avoid this errors? Obviously something is wrong with the sgmllib.py.

I tried some solutions from SOF:

*] Tried soup = BeautifulSoup(page, fromEncoding=<encoding of the page>)
Result: Dosent work, same errors.

*] Tried upgrading my sgmllib.py from a 2.7.2 version onto my 2.7.1 verision
Result: Dosent work, same errors.

*] Tried html = BeautifulSoup(page.encode('utf-8'))
Result: Dosent work, same errors.

I would appreciate any suggestions as to how to solve this encode error.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

新雨望断虹 2025-01-04 19:06:24

在您的模块中尝试此代码

if __name__ == "__main__":
    reload(sys)
    sys.setdefaultencoding("utf-8")

try this code in your module

if __name__ == "__main__":
    reload(sys)
    sys.setdefaultencoding("utf-8")
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文