【问题标题】:"NoneType object is not callable" error when getting web page获取网页时出现“NoneType 对象不可调用”错误
【发布时间】:2012-08-29 09:16:52
【问题描述】:

我对 python 很陌生,我写了这段代码

def geturl(url):
    url = urllib.quote(url,'/:')
    log( __name__ ,"Getting url:[%s]" % url)
    try:
        req = urllib2.Request(url)
        response = urllib2.urlopen(req)
        content    = response.read()
        return_url = response.geturl()
        response.close()
    except Exception, e:
        log( __name__ ,"Failed to get url because %s" % str(e))
        content    = None
        return_url = None
    return(content, return_url)

def myFunc():
    searchurl="http://www.google.com"
    socket.setdefaulttimeout(3)
    content, response_url = geturl(searchurl)
    if content is None:
        log( __name__ , "Content is None!!")
    content = content.replace("string1", "string2")

当我运行它时,我得到这个错误:NoneType 对象不可调用。 我不明白为什么......我了解到 NoneType 是 None 的类型,但我检查“content”变量是否为 None 并且不是。 谢谢你的帮助

【问题讨论】:

  • 哪一行代码出错了,能否粘贴回溯
  • 其他是none。如果不合逻辑,计算机就什么都不是。

标签: python download nonetype


【解决方案1】:

您没有给出错误的行,但是我怀疑以下内容是错误的:

if content is None:
    log( __name__ , "Content is None!!")
content = content.replace("string1", "string2")

content 确实是None 时,会发出日志,但函数会继续执行,直到对replace 的调用引发异常。

【讨论】:

  • 首先,谢谢您的回答。问题是内容不应该是无,并且没有写入日志行来说明它是。正如您已经发现的那样,给出错误的行是带有 content.replace 的行
猜你喜欢
  • 1970-01-01
  • 2023-04-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-01-23
  • 2018-12-03
  • 2017-05-30
相关资源
最近更新 更多