![](/img/trans.png)
[英]AttributeError: module 'pickle' has no attribute 'load' in python
[英]Python pickle instance has no attribute
我正在尝试抓取网站。 在此网站中,我将已爬网的人员存储在person_set中,并将用于爬网下一个人员的队列存储在parse_queue中。 在开始每个人爬网时,我需要将这两个数据结构写入文件中,以防由于异常或连接不良而导致爬网中断,因此我可以稍后继续。
我有三个python文件。 主文件,蜘蛛和人物模型。 Main实例化Spider,Spider开始解析,并在必要时调用write和read。 人员文件具有“人员”类,该类是用于存储人员数据的模型。
我在读取我写的数据时遇到问题。 我检查了许多有关此错误的问题,这似乎是导入问题。 但是,即使我将Person类导入到main和spider中,它仍然给我错误。 看来Emergency_read方法不受我的顶级导入的影响。
main.py
from spiders import Spider
from person import Person
import pickle
def main():
....
spider = Spider("seed_input")
spider.parse(client)
spiders.py
import pickle
from person import Person
class Spider:
def __init__(self, filename):
self.person_set = Set()
self.file_to_seed(filename)
for seed_url in self.seed_list:
self.seed_to_parse_queue(seed_url)
def parse(self, client):
if os.path.exists('tmp.person_set'):
print "Program wasnt ended properly, continuing from where it left"
self.emergency_read()
... starts parsing
def emergency_write(self):
if os.path.exists('tmp.person_set'):
self.delete_emergency_files()
with open('tmp.person_set', 'wb') as f:
pickle.dump(self.person_set, f)
with open('tmp.parse_queue', 'wb') as f:
pickle.dump(self.parse_queue, f)
def emergency_read(self):
with open('tmp.person_set', 'rb') as f:
self.person_set = pickle.load(f)
with open('tmp.parse_queue', 'rb') as f:
self.parse_queue = pickle.load(f)
person.py
class Person:
def __init__(self, name):
self.name = name
self.friend_set = Set()
self.profile_url = ""
self.id = 0
self.color = "Grey"
self.parent = None
self.depth = 0
def add_friend(self, friend):
self.friend_set.add(friend)
def __repr__(self):
return "Person(%s, %s)" % (self.profile_url, self.name)
def __eq__(self, other):
if isinstance(other, Person):
return ((self.profile_url == other.profile_url) and (self.name == other.name))
else:
return False
def __ne__(self, other):
return (not self.__eq__(other))
def __hash__(self):
return hash(self.__repr__())
堆栈跟踪
python main.py
Program wasnt ended properly, continuing from where it left
Traceback (most recent call last):
File "main.py", line 47, in <module>
main()
File "main.py", line 34, in main
spider.parse(client)
File "/home/ynscn/py-workspace/lll/spiders.py", line 39, in parse
self.emergency_read()
File "/home/ynscn/py-workspace/lll/spiders.py", line 262, in emergency_read
self.person_set = pickle.load(f)
File "/usr/lib/python2.7/pickle.py", line 1378, in load
return Unpickler(file).load()
File "/usr/lib/python2.7/pickle.py", line 858, in load
dispatch[key](self)
File "/usr/lib/python2.7/pickle.py", line 1198, in load_setitem
dict[key] = value
File "/home/ynscn/py-workspace/lll/person.py", line 30, in __hash__
return hash(self.__repr__())
File "/home/ynscn/py-workspace/lll/person.py", line 18, in __repr__
return "Person(%s, %s)" % (self.profile_url, self.name)
AttributeError: Person instance has no attribute 'profile_url'
Pickle以不确定的顺序加载类实例的组件。 在加载期间但在反序列化Person.profile_url
属性之前,会发生此错误。 请注意,它在load_setitem
期间load_setitem
,这意味着它可能正在尝试加载friend_set
属性(该属性是一个集合)。
您的自定义__repr__()
依赖于class属性,然后您的自定义__hash__()
( pickle
需要)依赖于__repr__()
。
我的建议是使用Python的默认__hash__
方法。 那行得通吗?
如果使用dill
而不是pickle
您的代码可能会按原样进行序列化。 dill
可以腌制类对象,实例,方法和属性……以及python中的大多数内容。 dill
还可以为类和类实例存储动态修改的状态。 我同意@nofinator指出,这似乎是一个泡菜load
错误。 但是, dill
可能会绕开它。
可能更好的是,如果您想强制执行装载和卸载的命令,则可以尝试添加__getstate__
和__setstate__
方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.