问题描述
的Mac OS X 10.9
Python 2.7版IDLE
BeautifulSoup 4安装(成功)
Mac OS X 10.9 Python 2.7 IDLEBeautifulSoup 4 installed (successfully)
我跟着并练习一些功能在IDLE 。下面code ++工程,并能打印出标题和放大器; title.name。
I followed BS4 documentation and was practicing some of the functions on IDLE. The following code works and was able to print out title & 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下一行:
But as I moved on and tried to print soup.title.string in the next line:
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
,然后我试图运行在终端在同一code:
AND THEN, I tried to run the same code on Terminal:
Python的[文件名]的.py
Python [filename].py
它的工作!
<title>The Dormouse's story</title>
title
The Dormouse's story
有人能解释为什么code没有空闲,但终端工作?谢谢!
Can someone explained why the code didn't work on IDLE but Terminal? Thank you!
推荐答案
这是一个已知的bug汇报了Python的问题跟踪器的
This was a known bug, reported on the Python issue tracker as issue #23583: IDLE: printing unicode subclasses broken (again).
这是几个月前固定的,2.7.10版本这应该不再发生如此开始。尝试更新你的Python!
This was fixed a few months ago, so beginning with version 2.7.10 this should no longer happen. Try updating your Python!
这篇关于BS4:&QUOT; soup.title.string&QUOT;在IDLE但终端不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!