None type对象没有属性-如何运行Python Beautiful Soup来传递None类型

None type对象没有属性-如何运行Python Beautiful Soup来传递None类型,python,beautifulsoup,web-crawler,Python,Beautifulsoup,Web Crawler,我是Python新手,因此如果有任何帮助,我将不胜感激。我有一个使用beautifulsoup的网络爬虫。它只适用于以下情况,并返回错误“None-type对象没有属性”。我知道这意味着它遇到了一个没有条目的页面。如何停止此错误并使其返回所有包含条目的其他页面。网络爬虫中的某些页面具有该条目,而某些页面为空白 我还将它们写入csv: 该怎么做才能使它通过none类型而不因错误而停止? 谢谢 您可以捕获异常并忽略特定的非类型错误: try: yourinstance.hello() exc

我是Python新手,因此如果有任何帮助,我将不胜感激。我有一个使用beautifulsoup的网络爬虫。它只适用于以下情况,并返回错误“None-type对象没有属性”。我知道这意味着它遇到了一个没有条目的页面。如何停止此错误并使其返回所有包含条目的其他页面。网络爬虫中的某些页面具有该条目,而某些页面为空白

我还将它们写入csv: 该怎么做才能使它通过none类型而不因错误而停止? 谢谢


您可以捕获异常并忽略特定的非类型错误:

try:
    yourinstance.hello()
except AttributeError as ae:
    if "NoneType" in ae:
        # ignore it
        pass
    else:
        print "error occur:" + str(ae)
或者,在实例不是none时调用method()

if yourInstance:
    yourInstance.method()


@hello11将其替换为您得到的非类型错误实例。如果我输入以下内容,则会出现无效语法:if yourInstance不是None:yourInstance.method(bbb=re.compile('First listed'))next_a=soup.find(text=bbb.parent.parent.get_text(strip=True)
if yourInstance:
    yourInstance.method()
if yourInstance is not None:
    yourInstance.method()