当我运行它时,终端一直在追赶" 23851"在新行中,这是我文件f中第一个Twitter名称的关注者数量;我相信这意味着指针没有在文件f中移动,但我不确定如何在Python中正确完成这一点2)当我检查文件f1时,没有任何东西,即程序是不按预期写入f1。
import tweepy
from tweepy import Stream
from tweepy.streaming import StreamListener
from tweepy import OAuthHandler
CONSUMER_KEY = 'xxx'
CONSUMER_SECRET = 'xxx'
ACCESS_KEY = 'xxx'
ACCESS_SECRET = 'xxx'
auth = OAuthHandler(CONSUMER_KEY,CONSUMER_SECRET)
api = tweepy.API(auth)
auth.set_access_token(ACCESS_KEY, ACCESS_SECRET)
#Create Class First
class TweetListener(StreamListener):
# A listener handles tweets are the received from the stream.
#This is a basic listener that just prints received tweets to standard output
def on_data(self, data): # indented inside the class
print(data)
return True
def on_error(self, status):
print(status)
# open both files outside the loop
with open('Twitternames.txt') as f,open('followers_number.txt', 'a') as f1:
for x in f:
#search
api = tweepy.API(auth)
twitterStream = Stream(auth,TweetListener())
test = api.lookup_users(screen_names=['x'])
for user in test:
print(user.followers_count)
#print it out and also write it into a file
s = user.followers_count
f1.write(str(s) +"\n") # add a newline with +
#end of stackoverflow
f.close()
答案 0 :(得分:0)
实际上有一些事情需要考虑,还有一些不需要的线路。所以我会逐行进行解释相关的事情,因为我们不需要任何流数据来计算关注者的数量,所以我们只需要导入tweepy和OauthHandler,所以:
import tweepy
from tweepy import OAuthHandler
现在我们需要设置登录所需的4个密钥,这将与:
相同CONSUMER_KEY = 'xxxxxxxx' #Replace with the original values.
CONSUMER_SECRET = 'xxx' #Replace with the original values.
ACCESS_KEY = 'xxx' #Replace with the original values.
ACCESS_SECRET = 'xxx' #Replace with the original values.
auth = OAuthHandler(CONSUMER_KEY,CONSUMER_SECRET)
auth.set_access_token(ACCESS_KEY, ACCESS_SECRET)
api = tweepy.API(auth)
我猜你不需要,StreamListner只记录各种用户的follower_count。所以我正在跳过那部分,但是之后你可以添加该代码片段。
usernames_file = open('Twitternames.txt').readlines()
我假设Twitternames.txt
的内容采用以下格式(每个用户名没有@
符号并以新行分隔):
user_name_1
user_name_2
user_name_3
...
现在usernames_file将是字符串usernames_file= ['user_name_1\n', 'user_name_2\n', 'user_name_3\n']
的列表,所以现在我们已经从文本文件中提取了各种用户名,但是我们需要在每个名称末尾删除\n
个字符。所以我们可以使用.strip()
方法。
usernames = []
for i in usernames_file:
usernames.append(i.strip())
>>> usernames = ['user_name_1', 'user_name_2', 'user_name_3']
现在我们已准备好使用lookup_users方法,因为此方法将用户名列表作为输入。
所以它可能看起来像这样:
test = api.lookup_users(screen_names=usernames)
for user in test:
print(user.followers_count)
如果要将结果记录到.txt
文件,则可以使用:
log_file = open("log.txt", 'a')
test = api.lookup_users(screen_names=usernames)
for user in test:
print(user.followers_count)
log_file.write(user.name+" has "+str(user.followers_count)+" followers.\n")
log_file.close()
所以短代码和最终代码看起来像这样:
import tweepy
from tweepy import OAuthHandler
CONSUMER_KEY = 'xxx'
CONSUMER_SECRET = 'xxx'
ACCESS_KEY = 'xxx'
ACCESS_SECRET = 'xxx'
auth = OAuthHandler(CONSUMER_KEY,CONSUMER_SECRET)
auth.set_access_token(ACCESS_KEY, ACCESS_SECRET)
api = tweepy.API(auth)
usernames_file = open('Twitternames.txt').readlines()
usernames = []
for i in usernames_file:
usernames.append(i.strip())
log_file = open("log.txt", 'a')
test = api.lookup_users(screen_names=usernames)
for user in test:
print(user.followers_count)
log_file.write(user.name+" has "+str(user.followers_count)+" followers.\n")
log_file.close()