如何在 Twitter API 中调用多个用户名?

问题描述 投票:0回答:1

我可以通过

get_all_tweets(user_name)
在Python中成功获取用户的数据。现在我想用同一代码从多个用户检索数据。

我尝试复制相同的代码并将其附加到代码中。我没有使用

user_name
,而是使用
user_name1
,我还尝试将所有其他变量更改为
...1
。 user_name 的列表仍然会生成。
user_name1
的列表不是。

import tweepy
import pandas as pd
import webbrowser
import time

def get_all_tweets(user_name):
    bearer_token ='XXX'
    client = tweepy.Client(bearer_token=bearer_token)

    user = client.get_user(username = user_name)
    user_id = user.data.id
    userTweets = []
    count = 0
    limit = 3000
    pagination_token = None
    start_time = '2023-03-01T00:00:00Z' # YYYY-MM-DDTHH:mm:ssZ
    end_time   = '2023-03-02T00:00:00Z' # YYYY-MM-DDTHH:mm:ssZ
    while count < limit:
        tweets = client.get_users_tweets(
            id = user_id,
            exclude = ['retweets','replies'],
            tweet_fields = ['created_at','public_metrics'],
            pagination_token = pagination_token,
            start_time = start_time,
            end_time = end_time,
            max_results = 100)
        if 'next_token' in tweets.meta.keys():
            if (tweets.meta['next_token']) is not None:
                pagination_token = tweets.meta['next_token']
            else:
                pagination_token = None
        else:
            pagination_token = None
        if tweets.data is not None:
            for tweet in tweets.data:
                count += 1
                userTweets.append({
                "id": tweet.id,
                "test": tweet.text,
                "date": tweet.created_at,
                "public":tweet.public_metrics
                })
        if pagination_token is None:
            break
    return userTweets, count

# user_name can assign your tweet name
user_name = 'Weltwoche'
[userTweets, count] = get_all_tweets(user_name)

def get_all_tweets(user_name1):
    bearer_token ='XXX'
    client = tweepy.Client(bearer_token=bearer_token)

    user = client.get_user(username = user_name1)
    user_id = user.data.id
    userTweets1 = []
    count1 = 0
    limit = 3000
    pagination_token = None
    start_time = '2023-03-01T00:00:00Z' # YYYY-MM-DDTHH:mm:ssZ
    end_time   = '2023-03-02T00:00:00Z' # YYYY-MM-DDTHH:mm:ssZ
    while count1 < limit:
        tweets1 = client.get_users_tweets(
            id = user_id,
            exclude = ['retweets','replies'],
            tweet_fields = ['created_at','public_metrics'],
            pagination_token = pagination_token,
            start_time = start_time,
            end_time = end_time,
            max_results = 100)
        if 'next_token' in tweets1.meta.keys():
            if (tweets1.meta['next_token']) is not None:
                pagination_token = tweets1.meta['next_token']
            else:
                pagination_token = None
        else:
            pagination_token = None
        if tweets1.data is not None:
            for tweet1 in tweets1.data:
                count1 += 1
                userTweets1.append({
                "id": tweet.id,
                "test": tweet.text,
                "date": tweet.created_at,
                "public":tweet.public_metrics
                })
        if pagination_token is None:
            break
    return userTweets1, count1

# user_name can assign your tweet name
user_name1 = 'Blick'
[userTweets1, count1] = get_all_tweets(user_name1)

print(count)
for tweet in userTweets:
    print(tweet)
print(count1)
for tweet1 in userTweets1:
    print(tweet1)
function twitter append twitterapi-python
1个回答
1
投票

使用用户数组并调用相同的函数。

import tweepy

def get_all_tweets(user_name):
    bearer_token ='your bearer token' # From https://developer.twitter.com/en/portal
    client = tweepy.Client(bearer_token=bearer_token)

    user = client.get_user(username = user_name)
    user_id = user.data.id
    userTweets = []
    count = 0
    limit = 3000
    pagination_token = None
    start_time = '2023-01-01T00:00:00Z' # YYYY-MM-DDTHH:mm:ssZ
    end_time   = '2023-01-31T00:00:00Z' # YYYY-MM-DDTHH:mm:ssZ
    while count < limit:
        tweets = client.get_users_tweets(
            id = user_id,
            exclude = ['retweets','replies'],
            pagination_token = pagination_token,
            start_time = start_time,
            end_time = end_time,
            max_results = 100)
        if 'next_token' in tweets.meta.keys():
            if (tweets.meta['next_token']) is not None:
                pagination_token = tweets.meta['next_token']
            else:
                pagination_token = None
        else:
            pagination_token = None
        if tweets.data is not None:
            for tweet in tweets.data:
                count += 1
                userTweets.append({
                    "id": tweet.id,
                    "test": tweet.text
                })
        if pagination_token is None:
            break
    return userTweets, count

# you can make users array in here
user_names = ["elonmusk", "BarackObama"]
for user_name in user_names:
    [tweets, count] = get_all_tweets(user_name)

    for tweet in tweets:
        tweet_url = f"https://twitter.com/{user_name}/status/{tweet['id']}"
        print(tweet)
        print(tweet_url)

    print(user_name + "'s tweets count is " + str(count))

结果

elonmusk's tweets count is 78
...
BarackObama's tweets count is 26

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.