首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >模块:“AttributeError”对象没有特性“”ValueError“”

模块:“AttributeError”对象没有特性“”ValueError“”
EN

Stack Overflow用户
提问于 2018-12-06 04:06:23
回答 2查看 1.2K关注 0票数 2

所以我在使用tld库时遇到了错误,因为它不知道如何处理某些代理请求urls。为了解决这个问题,添加了一些例外,并且它对特定天数的数据有效。

import tld
from tld import get_fld

#Custom try-except function to handle IPs and garbage http requests
def try_get_fld(x):
    try: 
        return get_fld(x)
    except tld.exceptions.TldBadUrl: 
        return np.nan
    except tld.exceptions.TldDomainNotFound:
        return np.nan

#Apply the function above to the request dataframe
request['flds'] = request['request'].apply(try_get_fld)

但在另一天,我遇到了一个新的错误:

ValueError: Invalid IPv6 URL

因此,我添加了一些异常:

def try_get_fld(x):
    try: 
        return get_fld(x)
    except tld.exceptions.TldBadUrl: 
        return np.nan
    except tld.exceptions.TldDomainNotFound:
        return np.nan
    except tld.exceptions.ValueError:
        return np.nan

然后我遇到了一个属性错误:

AttributeError: 'module' object has no attribute 'ValueError'

因此,我将其添加到异常中:

def try_get_fld(x):
    try: 
        return get_fld(x)
    except tld.exceptions.TldBadUrl: 
        return np.nan
    except tld.exceptions.TldDomainNotFound:
        return np.nan
    except tld.exceptions.ValueError:
        return np.nan
    except tld.exceptions.AttributeError:
        return np.nan

然后我又得到了AttributeError:'ValueError‘对象没有’ValueError‘属性。

有没有人知道我做错了什么或者如何解决我的问题?目标只是用NaN标记请求urls,这样我就可以将该方法应用于我的dataset。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2018-12-06 04:23:19

这是因为ValueError是一个内置异常,而不是tld的成员。使用except ValueError而不是tld.exceptions.ValueError

票数 1
EN

Stack Overflow用户

发布于 2018-12-06 04:32:12

您可以指定一个异常列表,使您的代码简洁。

def try_get_fld(x):
    try: 
        return get_fld(x)
    except (tld.exceptions.TldBadUrl, 
            tld.exceptions.TldDomainNotFound, 
            ValueError): 
        return np.nan
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/53639958

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档