我正在抓取网页上的某些数据然后我想将它保存到ZODB数据库。 刮:
STARTING_URL = "https://persoanefizice.otpbank.ro/ro/curs-valutar"
page = requests.get(STARTING_URL)
tree = html.fromstring(page.text)
z = tree.xpath('//tr/td[position()<5]/text()')
Scraping在一个脚本中完成,并通过调用它来保存到另一个数据库:
datainput.main(pres,nume,val_c,val_v,data_de_azi,'OTP')
我尝试调用时出现错误。给出以下代码:
STARTING_URL = "https://persoanefizice.otpbank.ro/ro/curs-valutar"
page = requests.get(STARTING_URL)
tree = html.fromstring(page.text)
z = tree.xpath('//tr/td[position()<5]/text()')
contor = 0
while contor < len(z) :
pres = z[contor]
nume = z[contor+1]
val_c = z[contor+2]
val_v = z[contor+3]
# pdb.set_trace()
datainput.main(pres,nume,val_c,val_v,data_de_azi,'OTP')
print z[contor],z[contor+1],z[contor+2],z[contor+3],data_de_azi,'OTP'
contor = contor + 4
我收到此错误消息:
TypeError: can't pickle ElementBase objects
完整追溯:
> datainput.main(pres,nume,val_c,val_v,data_de_azi,'OTP') File "/home/iulian/workspace/Python/Crawler/datainput.py", line 36, in main
> transaction.commit() File "/usr/local/lib/python2.7/dist-packages/transaction/_manager.py", line
> 111, in commit
> return self.get().commit() File "/usr/local/lib/python2.7/dist-packages/transaction/_transaction.py",
> line 280, in commit
> reraise(t, v, tb) File "/usr/local/lib/python2.7/dist-packages/transaction/_transaction.py",
> line 271, in commit
> self._commitResources() File "/usr/local/lib/python2.7/dist-packages/transaction/_transaction.py",
> line 417, in _commitResources
> reraise(t, v, tb) File "/usr/local/lib/python2.7/dist-packages/transaction/_transaction.py",
> line 391, in _commitResources
> rm.commit(self) File "/usr/local/lib/python2.7/dist-packages/ZODB/Connection.py", line 572,
> in commit
> self._commit(transaction) File "/usr/local/lib/python2.7/dist-packages/ZODB/Connection.py", line 628,
> in _commit
> self._store_objects(ObjectWriter(obj), transaction) File "/usr/local/lib/python2.7/dist-packages/ZODB/Connection.py", line 663,
> in _store_objects
> p = writer.serialize(obj) # This calls __getstate__ of obj File "/usr/local/lib/python2.7/dist-packages/ZODB/serialize.py", line 419,
> in serialize
> return self._dump(meta, obj.__getstate__()) File "/usr/local/lib/python2.7/dist-packages/ZODB/serialize.py", line 428,
> in _dump
> self._p.dump(state) File "/usr/lib/python2.7/copy_reg.py", line 70, in _reduce_ex
> raise TypeError, "can't pickle %s objects" % base.__name__ TypeError: can't pickle ElementBase objects
答案 0 :(得分:0)
解决了我的问题是将z []数据转换为字符串,然后将其保存在如下变量中:
pres = str(z[contor])
nume = str(z[contor+1])
val_c = str(z[contor+2])
val_v = str(z[contor + 3])