无法使用tweepy为特定用户获取推文

时间:2015-10-24 19:02:49

标签: python twitter tweepy

我是tweepy的新手。我收集了特定用户的所有关注者,并使用他们的ID,我试图提取他们的最后100条推文。我的代码能够为某些用户而不是其他用户提取推文。在一次尝试中,它能够为特定用户提取推文,并且在下一次尝试中它无法提取。

我的代码:

def get_api(cfg):
  auth = tweepy.OAuthHandler(cfg['consumer_key'], cfg['consumer_secret'])
  auth.set_access_token(cfg['access_token'], cfg['access_token_secret'])
  return tweepy.API(auth)

def getStatuses(api,id,statusLimit = 100 ):
  global TOTAL_REQUESTS
  statusFlag = True
  statusList = []
  page = 1
  screenName = None
  while statusFlag:
    for i in range(5):
      try:
          statuses = api.user_timeline(user_id = id,page = page,count=100)
          TOTAL_REQUESTS += 1
          break
      except:
          print " FAILED user " + str(id_user) + ", retrying"
      time.sleep(4)
    if len(statuses) > 0 and len(statusList) < statusLimit:
      tempList = [(status.id_str,str(status.retweet_count),str(status.favorite_count),status.created_at,str(status.place),str(status.source),status.text) for status in statuses]
      statusList.extend(tempList)
      if len(statusList) > statusLimit:
        statusFlag = False
      page+=1
    else:
      statusFlag = False
  print "Loaded user %d . total %s" % (id_user,len(statusList))
  return statusList

def getWordCounts(api,user,statusLimit):
  # Parse the user
  statuses = getStatuses(api,user,statusLimit)
  wc = {}

  #Loop over all the entries
  for e in statuses:
    text = e[6]
    print text
    #Extract a list of words
    words = getWords(text)
    for word in words:
      wc.setdefault(word,0)
      wc[word]+=1

  if len(statuses) > 0:
    return statuses[0][1], wc   
  else:
    return screen_name(api.lookup_users(user)), wc
def main():
  cfg = { 
    "consumer_key"        : "aaa",
    "consumer_secret"     : "bbb",
    "access_token"        : "ccc",
    "access_token_secret" : "ddd" 
    }

  api = get_api(cfg)
  #user = "bogas04"
  userids = []
  #have to append that user id in this
  for page in tweepy.Cursor(api.followers_ids,screen_name = "bogas04").pages():
      print page
      userids.extend(page)
  #userids.extend(api.me())

  output = open('usersData.pk1','wb')

  wordCounts = {}
  apCount = {}

  for u in userids:
    try:
      screeName,wc = getWordCounts(api,u,STATUSES_MAX)
      wordCounts[screeName] = wc
      print screeName + 'OK!'
      for word, count in wc.items():
        apCount.setdefault(word,0)
        if count > 1:
          apCount[word] += 1
    except:
      print 'Failed to parse the user %s' %u
  pickle.dump(wordCounts,output)
  pickle.dump(apCount,output)
  output.close()

main()

非常感谢任何形式的帮助。请帮忙!

0 个答案:

没有答案