这是代码:
select_sql = 'select url from baidu_v_tbl'
results = cur_oracle.execute(select_sql)
`enter code here`urls = results.fetchall()
logging.info('start........... ')
for con in urls:
url = con[0]
url_v = ''.join(['https://www.baidu.com/s?wd=', url, '@v']) mongo_records = db_mongo.keywords.find({'url': url_v},{'url': 1, 'keyword': 1}, no_cursor_timeout=True)
mongo_keywords = [key['keyword'] for key in mongo_records if key['keyword']]
这是错误:
Traceback (most recent call last): File "from_keywords_into_oracle.py", line 44, in <module> mongo_keywords = [key['keyword'] for key in mongo_records] KeyError: 'keyword'
答案 0 :(得分:0)
例外是明确的:&#34; KeyError&#39;关键字&#39;&#34;。
变量mongo_records
是dict
对象(或类似字母)的集合,您至少得到一个没有关键字&#39;关键字的项目。
您可以替换为:
mongo_keywords = [key['keyword'] for key in mongo_records if 'keyword' in key]