Matthew A.Russell의 "Mining the Social Web"이라는 책을 기반으로 모듈 twitter 및 redis를 사용하여 Python의 Twitter 데이터 마이닝 응용 프로그램을 작성하고 있습니다. 내가 레디 스를 사용하지 않는 경우Twitter Python 프로그램 연결 오류
raise ConnectionError(self._error_message(e))
ConnectionError: Error 10061 connecting localhost:6379. No connection could be made because the target machine actively refused it.
, 나는 모든 친구와 추종자의 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()
모든 도움은 정말 감사하겠습니다. 감사!
어떻게하면됩니까? – Nathan822
어떤 운영 체제를 사용하고 있습니까? 우분투 나 다른 데비안 유도체라면'sudo apt-get telnet을 설치하십시오' –
Windows 7. 텔넷 클라이언트를 설치하고 telnet localhost 6379 명령을 실행했습니다. 실패하면. 출력은 다음과 같습니다. localhost에 연결 중 ... 포트 6379에서 호스트에 대한 통신을 열 수 없습니다 : 연결에 실패했습니다. – Nathan822