python - 为 mongodb 中的网络抓取工具分配 UUID 并检查重复项

标签 python mongodb web-scraping scrapy pymongo

我正在构建一个网络抓取工具并尝试为实体分配 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...
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

控制台正在返回 KeyError: <pymongo.cursor.Cursor object at 0x104d41710> 。不确定如何解析 ent_uid 的光标

任何提示/建议表示赞赏!

最佳答案

Pymongo Find 命令返回一个光标对象,您需要迭代或访问才能获取该对象

访问第一个结果(您已经检查过结果是否存在),然后访问 ent_uid 字段。

据推测,您将使用 ent_eid 而不是 ent_uid 来搜索 EID 类型。如果您已经拥有它,则无需搜索。

ent_uid = db_coll.find({ent_eid_type: ent_eid })[0]['ent_uid']

或者不用担心光标,而是使用 find_one 命令 ( http://api.mongodb.com/python/current/api/pymongo/collection.html#pymongo.collection.Collection.find_one )

ent_uid = db_coll.find_one({ent_eid_type: ent_eid })['ent_uid']

关于python - 为 mongodb 中的网络抓取工具分配 UUID 并检查重复项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42565947/

相关文章:

python - 我想使用 scrapy python 单击网站链接

python - 如何将用 Pandas 提取的一列excel数据中的NaN替换为0

python - 如何在 tensorFlow 中重用 slim.arg_scope?

python : group elements together in list

node.js - 使用 $group stage 和 $sum 运算符聚合

mongodb - 在 Mongo 服务器端 javascript 中输出到控制台

php - 安装mongodb pecl软件包时出现问题:/tmp/pear/temp/mongodb/configure:./configure.lineno:第1行:语法错误:引号未终止

python - 获取特定值

html - VBA 宏 : problem to click a button (with class) on internet website (through internet explorer)

python - “列表”对象没有属性 'shape'