Skip to main content
 首页 » 编程设计

python之为什么我的非常简单的 Python 脚本失败了

2025年05月04日17yjmyzz

noob.py 在这里。我正在尝试从页面中获取内容,但 print 语句引发了一个我无法理解的错误。

实际代码:

import urllib2 
import sys 
 
url = "http://make.wordpress.org/core/page/2/" 
response = urllib2.urlopen(url) 
html = response.read 
print html 

输出:

$ python get.py 
<bound method _fileobject.read of <socket._fileobject object at 0x3722ec9a8d0>> 

我怀疑 Python 不喜欢那个特定的 URL,因为它与 http://www.python.org 一起工作,但我可以获得任何有用的信息明白这一点。

我也没有得到的是,如果我将其包含在 try: 和 except:↵ pass 中,我仍然会收到该错误消息。

非常喜欢任何指针。

请您参考如下方法:

这不是错误;它是 read 方法的字符串表示。

你看到它是因为在这条线上:

html = response.read 

您忘记调用read 方法。因此,html 被分配给方法本身,而不是它的返回值。

在方法名后添加 () 将调用它:

html = response.read() 

现在,html 被设置为 read 方法的返回值。