bs4: “soup.title.string” doesn't work on IDLE but Terminal

匿名 (未验证) 提交于 2019-12-03 01:48:02

问题:

Mac OS X 10.9 Python 2.7 IDLE BeautifulSoup 4 installed (successfully)

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 = """ The Dormouse's story

The Dormouse's story

Once upon a time there were three little sisters; and their names were Elsie, Lacie and Tillie; and they lived at the bottom of a well.

...

""" soup = BeautifulSoup(html) print soup.title print soup.title.name

Print result:

The Dormouse's story title 

But as I moved on and tried to print soup.title.string in the next line:

print soup.title.string 

It returned:

Traceback (most recent call last):   File "/Users/yumiyang/Documents/python-folder/bsoup_test.py", line 24, in      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 

AND THEN, I tried to run the same code on Terminal:

Python [filename].py

It worked!

The Dormouse's story title The Dormouse's story 

Can someone explained why the code didn't work on IDLE but Terminal? Thank you!

回答1:

This was a known bug, reported on the Python issue tracker as issue #23583: IDLE: printing unicode subclasses broken (again).

This was fixed a few months ago, so beginning with version 2.7.10 this should no longer happen. Try updating your Python!



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!