抱歉,试图理解并习惯字典和列表对象。
我通过他们的ebaysdk调用eBay的API,并希望将其中的项目存储为Mongo中的文档。简单。
以下是将返回的架构示例:
<timestamp>2009-09-04T00:47:12.456Z</timestamp>
<searchResult count="2">
<item>
<itemId>230371938681</itemId>
<title>Harry Potter and the Order of the Phoenix HD-DVD</title>
<globalId>EBAY-US</globalId>
<primaryCategory>
<categoryId>617</categoryId>
<categoryName>DVD, HD DVD & Blu-ray</categoryName>
</primaryCategory>
我已经尝试过500次这段代码的迭代,这里是我最基本的内容。
from ebaysdk import finding
from pymongo import MongoClient
api = finding(appid="billy-40d0a7e49d87")
api.execute('findItemsByKeywords', {'keywords': 'potter'})
listings = api.response_dict()
client = MongoClient('mongodb://user:pass@billy.mongohq.com:10099/ebaystuff')
db = client['ebaycollection']
ebay_collection = db.ebaysearch
for key in listings:
print key
ebay_collection.insert(key)
会收到此错误:
Traceback (most recent call last):
File "ebay_search.py", line 34, in <module>
ebay_collection.insert(key)
File "/Library/Python/2.7/site-packages/pymongo/collection.py", line 408, in insert
self.uuid_subtype, client)
File "/Library/Python/2.7/site-packages/pymongo/collection.py", line 378, in gen
doc['_id'] = ObjectId()
TypeError: 'str' object does not support item assignment
简单的东西。我想要做的就是将每个项目添加为文档。
答案 0 :(得分:2)
像字符串这样的不可变类型不能用作文档,因为它不允许添加其他字段,例如_id
field Mongo requires。您可以将字符串包装在字典中以用作包装文档:
key_doc = {'key': key}
ebay_collection.insert(key_doc)