集体智慧编程 - 发现群组 - 代码更改

P31 第一段代码报错说没有 title ,暂时不知道什么原因,但尝试更改:

def getwordcounts(url):
    d = feedparser.parse(url)
    wc = {}
    title = {}
    for e in d.entries:
        if 'summary' in e:
            summary = e.summary
        else:
            summary = e.description

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

P32 第一段代码中出现关键字 file ,但是 Python3 已弃用,尝试更改如下:

apcount = {}
wordcounts = {}
feedlist = [line for line in open('feedlist.txt')]
for feedurl in feedlist:
    title, wc = getwordcounts(feedurl)
    print(title)# 显示进度
    if title == {}:# 有的URL已经失效,将之跳过,否则将报错
        continue
    wordcounts[title] = wc
    for word, count in wc.items():
        apcount.setdefault(word, 0)
        if count > 1:
            apcount[word] += 1
不想麻烦的话可以直接使用官方提供的 blogdata.txt 文件


由于书中的例子均为英文版,有网站无法访问,有的可以访问但是分析聚类结果时很难一眼看出效果,因此本文选择了一些中文网站的 RSS 作为分析的初始 url 。



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值