作为披露,我对Python非常非常新。
我已经成功拉出了Twitter上列表成员的所有用户。我还根据屏幕名称提取了用户的所有推文 - 这两个组件都包含在下面。我如何组合这些,并拉出所有用户的所有推文?这甚至可能吗?以下所有内容:
#GOAL: pull all tweets from all users who are memberis of a list.
#imports necessary methods from Twitter library
import json
import tweepy
import time
import csv
import sys
#authorises twitter
CONSUMER_KEY = 'SECRET'
CONSUMER_SECRET = 'SECRET'
ACCESS_TOKEN = 'SECRET'
ACCESS_SECRET = 'SECRET'
#authorisations
auth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET)
auth.set_access_token(ACCESS_TOKEN, ACCESS_SECRET)
api = tweepy.API(auth, wait_on_rate_limit=True)
#returns members of a list & some details on them
for user in tweepy.Cursor(api.list_members, slug="uk-mps-labour", owner_screen_name="tweetminster", include_entities=True).items():
print(f"{user.id}\t{user.screen_name}\t{user.name}\t{user.description}\t{user.location}\t{user.followers_count}\t{user.friends_count}\t{user.verified}")
#creates a loop to iterate through the list of user ids
#returns all tweets of a user
counter = 0 #establishes a counter to number tweets output
for status in tweepy.Cursor(api.user_timeline, screen_name="frogface", tweet_mode="extended").items():
counter = counter + 1
print(f"{counter}\t{status.user.id}\t{status.user.screen_name}\t{status.created_at}\t{status.full_text}")
答案 0 :(得分:0)
当您遍历列表的用户而不是打印用户详细信息时,请将screen_name添加到列表中。
接下来遍历screen_names列表,然后获取用户的推文。代码看起来像这样:
screen_names = []
#returns members of a list & some details on them
for user in tweepy.Cursor(api.list_members, slug="uk-mps-labour", owner_screen_name="tweetminster", include_entities=True).items():
screen_names.append(f"{user.screen_name}")
for i in screen_names:
#returns all tweets of a user
counter = 0 #establishes a counter to number tweets output
for status in tweepy.Cursor(api.user_timeline, screen_name=i, tweet_mode="extended").items():
counter = counter + 1
print(f"{counter}\t{status.user.id}\t{status.user.screen_name}\t{status.created_at}\t{status.full_text}")