RSS 피드의 항목을 읽고 JSON 형식의 개별 항목을 CouchDB 데이터베이스에 저장하는 스크립트가 있습니다.Python CouchDB는 피드 파서 항목에서 생성 된 dict를 저장할 수 없습니까? ('no'속성)
는내 코드의 흥미로운 부분은 다음과 같은 :
Feed = namedtuple('Feed', ['name', 'url'])
couch = couchdb.Server(COUCH_HOST)
couch.resource.credentials = (COUCH_USER, COUCH_PASS)
db = couch['raw_entries']
for feed in map(Feed._make, csv.reader(open("feeds.csv", "rb"))):
d = feedparser.parse(feed.url)
for item in d.entries:
db.save(item)
내가 그 코드를 실행하려고 할 때, 나는 db.save(item)
에서 다음과 같은 오류 얻을 :
AttributeError: object has no attribute 'read'
확인을, 그래서 그런 다음 약간의 디버깅을했습니다 ...
for feed in map(Feed._make, csv.reader(open("feeds.csv", "rb"))):
d = feedparser.parse(feed.url)
for item in d.entries:
print(type(item))
결과는 <class 'feedparser.FeedParserDict'>
입니다. - 아아, feedparser가 자체 dict 유형을 사용하고 있습니다 ... 음, 명시 적으로 캐스팅하려고하면 dict
?
for feed in map(Feed._make, csv.reader(open("feeds.csv", "rb"))):
d = feedparser.parse(feed.url)
for item in d.entries:
db.save(dict(item))
Traceback (most recent call last):
File "./feedchomper.py", line 32, in <module>
db.save(dict(item))
File "/home/dealpref/lib/python2.7/couchdb/client.py", line 407, in save
_, _, data = func(body=doc, **options)
File "/home/dealpref/lib/python2.7/couchdb/http.py", line 399, in post_json
status, headers, data = self.post(*a, **k)
File "/home/dealpref/lib/python2.7/couchdb/http.py", line 381, in post
**params)
File "/home/dealpref/lib/python2.7/couchdb/http.py", line 419, in _request
credentials=self.credentials)
File "/home/dealpref/lib/python2.7/couchdb/http.py", line 239, in request
resp = _try_request_with_retries(iter(self.retry_delays))
File "/home/dealpref/lib/python2.7/couchdb/http.py", line 196, in _try_request_with_retries
return _try_request()
File "/home/dealpref/lib/python2.7/couchdb/http.py", line 222, in _try_request
chunk = body.read(CHUNK_SIZE)
AttributeError: 'dict' object has no attribute 'read'
w-what? 내가 여기서 무엇을 놓치고
some_dict = dict({'foo': 'bar'})
print(type(some_dict))
db.save(some_dict)
다음 작품 잘와 유형이 여전히 dict
때문에 즉, 이해가되지 않습니다?
이러한 오류에 대한 스택 추적을 게시 할 수 있습니까? 오류가 CouchDB 모듈에서 어딘가에있을 가능성이 있습니다. 'dict' 객체는'read()'메소드를 가지고 있지 않지만, 빨간 청어가 될 수 있습니다. – kindall
@kindall - stacktrace 전체를 게시했습니다 ... CouchDB가 어떤 이유로 든 청크 업로드를하려고하는 것 같습니다 (아마도 dict가 크기 때문에)? 그러나, 손에서 dict을 구성하여 동작을 복제 할 수 없습니다 (즉, 손으로 직접 작성하면 절약 할 수 있습니다 ...). – ashgromnies
네, 당신의 딕트가 어떤 이유로 든 파일이라고 생각하는 것 같습니다. 매우 이상합니다. – kindall