Python x Tweepy:如何从列表中包含的所有用户中提取推文
Python x Tweepy: how to pull tweets from all users contained within a list
作为披露,我对 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}")
当您遍历列表的用户而不是打印用户详细信息时,将 screen_name 添加到列表中。
next 遍历 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}")
作为披露,我对 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}")
当您遍历列表的用户而不是打印用户详细信息时,将 screen_name 添加到列表中。
next 遍历 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}")