이 발생하는 문제에 문의하시기 바랍니다 아무것도가 필요한 경우
, 나는 믿고 완전히 파이썬에서 MongoDB를에 삽입하기 전에 UTF-8 형식으로 변환하지 이스케이프 문자 때문이다.
MongoDB 변경 로그를 확인하지 않았지만 v.2.2 이상에서 올바르게 기억한다면 전체 유니 코드를 지원해야합니다.
어쨌든 mongoDB 2.6의 최신 버전으로 업그레이드하거나 scrapy-mongodb
스크립트를 수정/덮어 쓰는 2 가지 방법이 있습니다.
# ... previous code ...
key = {}
if isinstance(self.config['unique_key'], list):
for k in dict(self.config['unique_key']).keys():
key[k] = item[k]
else:
key[self.config['unique_key']] = item[self.config['unique_key']]
self.collection.update(key, item, upsert=True)
# ... and the rest ...
이 문제를 해결하려면, 당신은 process_item
함수 내에서이 몇 줄을 추가 할 수 있습니다 : K가 MongoDB를에 삽입하기 전에 UTF-8로 변환되지 않고,이 라인을 찾습니다 scrapy_mongodb.py
을 변경하려면
을
# ... previous code ...
def process_item(self, item, spider):
""" Process the item and add it to MongoDB
:type item: Item object
:param item: The item to put into MongoDB
:type spider: BaseSpider object
:param spider: The spider running the queries
:returns: Item object
"""
item = dict(self._get_serialized_fields(item))
# add a recursive function to convert all unicode to utf-8 format
# take this snippet from this [SO answer](http://stackoverflow.com/questions/956867/how-to-get-string-objects-instead-of-unicode-ones-from-json-in-python)
def byteify(input):
if isinstance(input, dict):
return {byteify(key):byteify(value) for key,value in input.iteritems()}
elif isinstance(input, list):
return [byteify(element) for element in input]
elif isinstance(input, unicode):
return input.encode('utf-8')
# if above utf-8 conversion still not working, replace them completely
# return input.encode('ASCII', 'ignore')
else:
return input
# finally replace the item with this function
item = byteify(item)
# ... rest of the code ... #
여전히 작동하지 않는 경우 mongodb를 최신 버전으로 업그레이드하는 것이 좋습니다.
희망이 도움이됩니다.
문서 중 하나를 삽입하려고했는데 어떤 오류도없이 mongodb의 어떤 버전을 사용하고 있으며 데이터베이스에 문서를 어떻게 삽입하고 있습니까? –
버전 2.4.6을 사용하고 있습니다. 예제는 삽입하려는 문서가 아니라 문서에 중첩 된 객체입니다. 전체 문서를 업로드하겠습니다. – ranisalt
여기에 있습니다 : https://gist.github.com/ranisalt/d7320d6993664e87b7c0 이것은 삽입 될 전체 문서입니다 – ranisalt