bs4:“soup.title.string”不适用于IDLE而是终端

时间:2014-06-18 22:23:15

标签: string terminal beautifulsoup osx-mavericks python-idle

Mac OS X 10.9 Python 2.7 IDLE BeautifulSoup 4安装(成功)

我关注BS4 documentation并正在练习IDLE 上的一些功能。以下代码有效,并且可以打印出标题& title.name。

from bs4 import BeautifulSoup

html = """
<html><head><title>The Dormouse's story</title></head>
<body>
<p class="title"><b>The Dormouse's story</b></p>

<p class="story">Once upon a time there were three little sisters; and their names were
<a href="http://example.com/elsie" class="sister" id="link1">Elsie</a>,
<a href="http://example.com/lacie" class="sister" id="link2">Lacie</a> and
<a href="http://example.com/tillie" class="sister" id="link3">Tillie</a>;
and they lived at the bottom of a well.</p>

<p class="story">...</p>
"""

soup = BeautifulSoup(html)


print soup.title
print soup.title.name

打印结果:

<title>The Dormouse's story</title>
title

但是当我继续前进并尝试在下一行打印soup.title.string时:

print soup.title.string

它返回了:

Traceback (most recent call last):
  File "/Users/yumiyang/Documents/python-folder/bsoup_test.py", line 24, in <module>
    print soup.title.string
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/idlelib/PyShell.py", line 1344, in write
    s = unicode.__getslice__(s, None, None)
TypeError: an integer is required

然后,我尝试在终端上运行相同的代码:

Python [filename] .py

有效!

<title>The Dormouse's story</title>
title
The Dormouse's story

有人可以解释为什么代码在IDLE上没有工作但终端?谢谢!

1 个答案:

答案 0 :(得分:0)

这是一个已知错误,在Python问题跟踪器上报告为issue #23583: IDLE: printing unicode subclasses broken (again)

这是几个月前修复的,所以从版本2.7.10开始,这应该不再发生了。尝试更新你的Python!