编程语言
首页 > 编程语言> > Python CouchDB无法保存从feedparser条目创建的dict? (没有属性’读’)

Python CouchDB无法保存从feedparser条目创建的dict? (没有属性’读’)

作者:互联网

我有一个脚本,我想读取RSS源中的条目,并将各个条目以JSON格式存储到CouchDB数据库中.

我的代码中有趣的部分看起来像这样:

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-什么?这没有意义,因为以下工作正常,类型仍然是dict:

some_dict = dict({'foo': 'bar'})
print(type(some_dict))
db.save(some_dict)

我在这里错过了什么?

解决方法:

我找到了一种方法,通过将结构序列化为JSON,然后回到我传递给CouchDB的Python字典 – 然后将其重新序列化为JSON以保存(是的,奇怪而不是有利,但它有效吗?)

我必须为转储执行自定义序列化方法,因为无法对eval’d的time_struct的repr进行评估.

资料来源:http://diveintopython3.org/serializing.html

码:

#!/usr/bin/env python2.7

from collections import namedtuple
import csv
import json
import time

import feedparser
import couchdb

def to_json(python_object):
    if isinstance(python_object, time.struct_time):
        return {'__class__': 'time.asctime',
                '__value__': time.asctime(python_object)}

    raise TypeError(repr(python_object) + ' is not JSON serializable')

Feed = namedtuple('Feed', ['name', 'url'])

COUCH_HOST = 'http://mycouch.com'
COUCH_USER = 'user'
COUCH_PASS = 'pass'

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:
        j = json.dumps(item, default=to_json)
        db.save(json.loads(j))

标签:python,couchdb,feedparser
来源: https://codeday.me/bug/20190723/1516530.html