尝试执行我认为非常基本的练习。我有7000多个客户的列表,我想从twitter导入他们的一些属性。我试图弄清楚如何在我的tweepy查询中输入几个用户名,但我在下面没有任何运气。
#!/usr/bin/python
import tweepy
import csv #Import csv
import os
# Consumer keys and access tokens, used for OAuth
consumer_key = 'MINE'
consumer_secret = 'MINE'
access_token = 'MINE'
access_token_secret = 'MINE'
# OAuth process, using the keys and tokens
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
api = tweepy.API(auth)
# Open/Create a file to append data
csvFile = open('ID.csv', 'a')
#Use csv Writer
csvWriter = csv.writer(csvFile)
users = ('IBM' or 'massmutual')
user = api.get_user(screen_name = users)
csvWriter.writerow([user.screen_name, user.id, user.followers_count, user.description.encode('utf-8')])
print user.id
csvFile.close()
答案 0 :(得分:2)
你必须在循环中完成它,你将制作用户列表并逐个获取所有用户实例。
#!/usr/bin/python
import tweepy
import csv #Import csv
import os
# Consumer keys and access tokens, used for OAuth
consumer_key = 'MINE'
consumer_secret = 'MINE'
access_token = 'MINE'
access_token_secret = 'MINE'
# OAuth process, using the keys and tokens
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
api = tweepy.API(auth)
# Open/Create a file to append data
csvFile = open('ID.csv', 'a')
#Use csv Writer
csvWriter = csv.writer(csvFile)
users = ['IBM','massmutual','user3',.......]
for user_name in users:
user = api.get_user(screen_name = user_name)
csvWriter.writerow([user.screen_name, user.id, user.followers_count, user.description.encode('utf-8')])
print user.id
csvFile.close()