python - 我收到了类型错误 : 'NoneType' object is not iterable

标签 python error-handling

功能中 getLink(urls) , 我有 return (cloud,parent,children)在主函数中,我有 (cloud,parent,children) = getLink(urls)我得到了这一行的错误:TypeError: 'NoneType' object is not iterable

亲子都是list http 链接。因为,它不能在这里粘贴它们,父是一个包含大约 30 个链接的列表; children 是一个包含大约 30 个项目的列表,每个项目大约有 10-100 个链接,由“,”分隔。

cloud 是一个包含大约 100 个单词的列表,例如:['official store', 'Java Applets Centre', 'About Google', 'Web History'.....]

我不知道为什么我收到错误。传递参数有什么问题吗?还是因为列表占用了太多空间?

#crawler url: read webpage and return a list of url and a list of its name
def crawler(url):
    try:
        m = urllib.request.urlopen(url)
        msg = m.read()
....
        return (list(set(list(links))),list(set(list(titles))) )
    except Exception:
        print("url wrong!")

#this is the function has gone wrong: it throw an exception here, also the error I mentioned, also it will end while before len(parent) reach 100. 
def getLink(urls):
    try:
        newUrl=[]
        parent = []
        children =[]
        cloud =[]
        i=0

        while len(parent)<=100:

            url = urls[i]
            if url in parent:
                i += 1
                continue
            (links, titles) = crawler(url)
            parent.append(url)
            children.append(",".join(links))
            cloud = cloud + titles
            newUrl= newUrl+links
            print ("links: ",links)
            i += 1
            if i == len(urls):
                urls = list(set(newUrl))
                newUrl = []
                i = 0

        return (cloud,parent,children)
    except Exception:
        print("can not get links")


def readfile(file):
    #not related, this function will return a list of url 

def main():
    file='sampleinput.txt'
    urls=readfile(file)
    (cloud,parent,children) = getLink(urls)

if __name__=='__main__':
    main()

最佳答案

可能有一种方法可以让您的函数在没有到达显式 return 的情况下结束。陈述。

看下面的示例代码。

def get_values(x):
    if x:
        return 'foo', 'bar'

x, y = get_values(1)
x, y = get_values(0)

当使用 0 调用该函数时作为参数 return被跳过,函数将返回 None .

您可以添加显式 return作为函数的最后一行。在此答案中给出的示例中,它看起来像这样。
def get_values(x):
    if x:
        return 'foo', 'bar'
    return None, None

看到代码后更新

当在 get_link 中触发异常时你只需打印一些东西并从函数返回。您没有 return语句,所以 Python 将返回 None .调用函数现在尝试扩展 None分成三个值,但失败了。

更改您的异常处理以返回一个包含三个值的元组,就像您在一切正常时所做的那样。使用 None对于每个值都是一个好主意,因为它向您表明出了问题。此外,我不会在函数中打印任何内容。不要混合业务逻辑和输入/输出。
except Exception:
    return None, None, None

然后在您的主要功能中使用以下内容:
cloud, parent, children = getLink(urls)
if cloud is None:
    print("can not get links")
else:
    # do some more work

关于python - 我收到了类型错误 : 'NoneType' object is not iterable,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26670912/

相关文章:

python - Django - 如何简单地获取域名?

asp.net - 默认Internet应用程序ASP.NET MVC中如何显示错误页面

c# - C#在另一个函数中覆盖try-catch

c# - 错误处理超过重试次数 10

python - 我应该在尝试/异常(exception)之后使用finally吗?

php - 保持输入值在提交时有错误,并在提交成功时清除它们(php)

python - Pandas:如何移动所有数据框

python - virtualenv 和 CLI 工具

python - 如何合并pandas中的两个数据框?

python - 使用 TensorFlow 实现 CNN