Python 在mongodb中为web scraper分配UUID并检查重复项
我正在构建一个web scraper并尝试为一个实体分配一个UUID 由于一个实体可能在不同的时间被刮取,我想将初始UUID与从网页中提取的id一起存储Python 在mongodb中为web scraper分配UUID并检查重复项,python,mongodb,web-scraping,scrapy,pymongo,Python,Mongodb,Web Scraping,Scrapy,Pymongo,我正在构建一个web scraper并尝试为一个实体分配一个UUID 由于一个实体可能在不同的时间被刮取,我想将初始UUID与从网页中提取的id一起存储 // example document { "ent_eid_type": "ABC-123", "ent_uid_type": "123e4567-aaa-123e456" } 下面是为在已删除项中找到的每个id字段运行的代码 # if the current ent_eid_type is a key in mongo... i
// example document
{
"ent_eid_type": "ABC-123",
"ent_uid_type": "123e4567-aaa-123e456"
}
下面是为在已删除项中找到的每个id字段运行的代码
# if the current ent_eid_type is a key in mongo...
if db_coll.find({ent_eid_type: ent_eid}).count() > 0:
# return the uid value
ent_uid = db_coll.find({ent_uid_type: ent_uid })
else:
# create a fresh uid
ent_uid = uuid.uuid4()
# store it with the current entity eid as key, and uid as value
db_coll.insert({ent_eid_type: ent_eid, ent_uid_type: ent_uid})
# update the current item with the stored uid for later use
item[ent_uid_type] = ent_uid
控制台正在返回键错误:
。不确定如何解析ent\u uid
任何提示/建议,不胜感激 Pymongo-Find命令返回一个光标对象,您需要迭代或访问该对象 访问第一个结果(您已检查是否存在一个),然后访问ent\u uid字段 大概,您将在EID类型上搜索,而ent_EID不是ent_uid。如果您已经拥有,则无需搜索
ent_uid = db_coll.find({ent_eid_type: ent_eid })[0]['ent_uid']
或者不用担心光标,而是使用find_one命令()
ent_uid = db_coll.find_one({ent_eid_type: ent_eid })['ent_uid']