0

我正在使用 Python 中的 Twitter 数据挖掘应用程序,使用模块 twitter 和 redis,基于 Matthew A.Russell 的“挖掘社交网络”一书。每当我尝试使用 redis 模块获取朋友和关注者的信息时,都会出现以下错误:

raise ConnectionError(self._error_message(e))
ConnectionError: Error 10061 connecting localhost:6379. No connection could be made because the target machine actively refused it.

当我不使用 Redis 时,我可以获取所有朋友和关注者的 id。但是我需要 Redis 对数据进行某些分析。我的源代码如下:

import json
import locale
import redis
from prettytable import PrettyTable

# Pretty printing numbers
from twitter__util import pp 

# These functions create consistent keys from 
# screen names and user id values
from twitter__util import getRedisIdByScreenName 
from twitter__util import getRedisIdByUserId

SCREEN_NAME = "timoreilly"

locale.setlocale(locale.LC_ALL, '')

def calculate():
    r = redis.Redis()  # Default connection settings on localhost

    follower_ids = list(r.smembers(getRedisIdByScreenName(SCREEN_NAME,
                        'follower_ids')))

    followers = r.mget([getRedisIdByUserId(follower_id, 'info.json')
                       for follower_id in follower_ids])
    followers = [json.loads(f) for f in followers if f is not None]

    freqs = {}
    for f in followers:
        cnt = f['followers_count']
        if not freqs.has_key(cnt):
            freqs[cnt] = []

        freqs[cnt].append({'screen_name': f['screen_name'], 'user_id': f['id']})

    # It could take a few minutes to calculate freqs, so store a snapshot for later use

    r.set(getRedisIdByScreenName(SCREEN_NAME, 'follower_freqs'),
          json.dumps(freqs))

    keys = freqs.keys()
    keys.sort()

    print 'The top 10 followers from the sample:'

    fields = ['Date', 'Count']
    pt = PrettyTable(fields=fields)
    [pt.set_field_align(f, 'l') for f in fields]

    for (user, freq) in reversed([(user['screen_name'], k) for k in keys[-10:]
                                    for user in freqs[k]]):
        pt.add_row([user, pp(freq)])

    pt.printt()

    all_freqs = [k for k in keys for user in freqs[k]]
    avg = reduce(lambda x, y: x + y, all_freqs) / len(all_freqs)

    print "\nThe average number of followers for %s's followers: %s" \
        % (SCREEN_NAME, pp(avg))

# psyco can only compile functions, so wrap code in a function

try:
    import psyco
    psyco.bind(calculate)
except ImportError, e:
    pass  # psyco not installed

calculate()

任何和所有的帮助将不胜感激。谢谢!

4

1 回答 1

1

那是 Redis 运行的端口。如果我是你,我会仔细检查 Redis 服务器是否在正确的端口和接口上运行。要对此进行测试,请确保您已安装 telnet 客户端并执行以下操作:

$ telnet localhost 6379

如果失败了,那么 Redis 服务器实际上并没有在 Redis 客户端库默认情况下运行,在这种情况下,您必须在构造函数参数中为客户端提供正确的详细信息。

于 2012-12-03T16:28:55.227 回答