【问题标题】:AttributeError: object has no attribute 'title'AttributeError:对象没有属性“标题”
【发布时间】:2015-03-16 01:50:49
【问题描述】:

我正在学习编程集体智慧这本书。以下是我的代码:

import feedparser
import re

# Returns title and dictionary of word counts for an RSS feed
def getwordcounts(url):
    # Parse the feed
    d = feedparser.parse(url)
    wc={}

    # Loop over all the entries
    for e in d.entries:
        if 'summary' in e:
            summary = e.summary
        else:
            summary = e.description

        # Extract a list of words
        words = getwords(e.title + '' + summary)
        for word in words:
            wc.setdefault(word, 0)
            wc[word] += 1
    return d.feed.title, wc

def getwords(html):
    # Remove all the HTML tags
    txt = re.compile(r'[^>]+>').sub('',html)

    # Split words by all non-alpha characters
    words = re.compile(r'[^A-Z^a-z]+').split(txt)

    # Convert to lowercase
    return [word.lower() for word in words if word!='']


apcount = {}
wordcounts = {}
for feedurl in file('feedlist.txt'):
    title, wc = getwordcounts(feedurl)
    wordcounts[title] = wc
    for word, count in wc.items():
        apcount.setdefault(word, 0)
        if count>1:
            apcount[word] += 1

wordlist = []
for w, bc in apcount.items():
    frac = float(bc)/len(feedlist)
    if frac>0.1 and frac<0.5:
        wordlist.append(w)

out = file('blogdata.txt', 'w')
out.write('Blog')
for word in wordlist:
    out.write('\t%s' % word)
out.write('\n')
for blog, wc in wordcounts.items(): 
    out.write(blog)
    for word in wordlist:
        if word in wc:
            out.wirte('\t%d' % wc[word])
        else:
            out.write('\t0')
    out.write('\n')

当我运行这个脚本时,我得到了消息:

Traceback (most recent call last):
  File "generatefeedvector.py", line 38, in <module>
    title, wc = getwordcounts(feedurl)
  File "generatefeedvector.py", line 22, in getwordcounts
    return d.feed.title, wc
  File "build/bdist.linux-x86_64/egg/feedparser.py", line 416, in __getattr__
AttributeError: object has no attribute 'title'

我检查了 feedparser 的版本是 5.1.3。

那么如何解决这个问题呢? 谢谢

【问题讨论】:

    标签: python machine-learning feedparser


    【解决方案1】:

    您尝试使用 feedparser 解析的 URL 不是有效的提要(使用 feedvalidator 检查),而是网页,或者提要为空,或者 title 为空。

    作为一种解决方法,请使用getattr()

    return getattr(d.feed, 'title', 'Unknown title'), wc
    

    另见:

    【讨论】:

    • 非常感谢您的回答。
    猜你喜欢
    • 1970-01-01
    • 2019-08-28
    • 2021-02-06
    • 2020-03-25
    • 2019-07-25
    • 1970-01-01
    • 2012-12-01
    • 2021-04-19
    • 2021-11-22
    相关资源
    最近更新 更多