所以我在使用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。
发布于 2018-12-06 04:23:19
这是因为ValueError
是一个内置异常,而不是tld
的成员。使用except ValueError
而不是tld.exceptions.ValueError
。
发布于 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
https://stackoverflow.com/questions/53639958
复制相似问题