gpt4 book ai didi

string - BS4 : "soup.title.string" doesn't work on IDLE but Terminal

转载 作者:行者123 更新时间:2023-12-02 06:50:51 25 4
gpt4 key购买 nike

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

我关注了BS4 documentation并正在在IDLE上练习一些功能。以下代码有效并且能够打印出 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 时:

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 [文件名].py

成功了!

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

有人可以解释为什么代码在 IDLE 上不起作用,但在终端上不起作用吗?谢谢!

最佳答案

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

该问题已于几个月前修复,因此从版本 2.7.10 开始,这种情况应该不会再发生。尝试更新你的 Python!

关于string - BS4 : "soup.title.string" doesn't work on IDLE but Terminal,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24295950/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com