Сканирование Twitter API для определенных c твитов - PullRequest
1 голос
/ 09 мая 2020
• 1000 написали как
PLACE_LAT = 29.7604
PLACE_LON = -95.3698
PLACE_RAD = 200

Затем я попытался применить функцию, чтобы найти не менее 200 твитов, но я знаю, что с каждым запросом можно искать только 100. Мой код пока что ниже, однако этот код не сработал.

def retrieve_tweets(api, keyword, batch_count, total_count, latitude, longitude, radius):
    """
    collects tweets using the Twitter search API

    api:         Twitter API instance
    keyword:     search keyword
    batch_count: maximum number of tweets to collect per each request
    total_count: maximum number of tweets in total
    """


    # the collection of tweets to be returned
    tweets_unfiltered = []
    tweets = []

    # the number of tweets within a single query
    batch_count = str(batch_count)

    '''
    You are required to insert your own code where instructed to perform the first query to Twitter API.
    Hint: revise the practical session on Twitter API on how to perform query to Twitter API.
    '''
    # per the first query, to obtain max_id_str which will be used later to query sub
    resp = api.request('search/tweets', {'q': keywords,
                                         'count': '100',
                                         'lang':'en',
                                         'result_type':'recent',
                                         'geocode':'{PLACE_LAT},{PLACE_LONG},{PLACE_RAD}mi'.format(latitude, longitude, radius)})

    # store the tweets in a list

    # check first if there was an error
    if ('errors' in resp.json()):
        errors = resp.json()['errors']
        if (errors[0]['code'] == 88):
            print('Too many attempts to load tweets.')
            print('You need to wait for a few minutes before accessing Twitter API again.')

    if ('statuses' in resp.json()):
        tweets_unfiltered += resp.json()['statuses']
        tweets = [tweet for tweet in tweets_unfiltered if ((tweet['retweeted'] != True) and ('RT @' not in tweet['text']))]

        # find the max_id_str for the next batch
        ids = [tweet['id'] for tweet in tweets_unfiltered]
        max_id_str = str(min(ids))

        # loop until as many tweets as total_count is collected
        number_of_tweets = len(tweets)
        while number_of_tweets < total_count:

            resp = api.request('search/tweets', {'q': keywords,
                                                 'count': '50',
                                                 'lang':'en',
                                                 'result_type': 'recent',
                                                 'max_id': max_id_str,
                                                 'geocode':'{PLACE_LAT},{PLACE_LONG},{PLACE_RAD}mi'.format(latitude, longitude, radius)}
                          )

            if ('statuses' in resp.json()):
                tweets_unfiltered += resp.json()['statuses']
                tweets = [tweet for tweet in tweets_unfiltered if ((tweet['retweeted'] != True) and ('RT @' not in tweet['text']))]

                ids = [tweet['id'] for tweet in tweets_unfiltered]
                max_id_str = str(min(ids))

                number_of_tweets = len(tweets)

            print("{} tweets are collected for keyword {}. Last tweet created at {}".format(number_of_tweets, 
                                                                                    keyword, 
                                                                                    tweets[number_of_tweets-1]['created_at']))
    return tweets

Мне нужно было только написать код, в котором говорилось: # Вставьте свой код. Какие изменения мне нужно внести, чтобы это работало

def retrieve_tweets(api, keyword, batch_count, total_count, latitude, longitude, radius):
    """
    collects tweets using the Twitter search API

    api:         Twitter API instance
    keyword:     search keyword
    batch_count: maximum number of tweets to collect per each request
    total_count: maximum number of tweets in total
    """


    # the collection of tweets to be returned
    tweets_unfiltered = []
    tweets = []

    # the number of tweets within a single query
    batch_count = str(batch_count)

    '''
    You are required to insert your own code where instructed to perform the first query to Twitter API.
    Hint: revise the practical session on Twitter API on how to perform query to Twitter API.
    '''
    # per the first query, to obtain max_id_str which will be used later to query sub
    resp = api.request('search/tweets', {'q': #INSERT YOUR CODE
                                         'count': #INSERT YOUR CODE
                                         'lang':'en',
                                         'result_type':'recent',
                                         'geocode':'{},{},{}mi'.format(latitude, longitude, radius)})

    # store the tweets in a list

    # check first if there was an error
    if ('errors' in resp.json()):
        errors = resp.json()['errors']
        if (errors[0]['code'] == 88):
            print('Too many attempts to load tweets.')
            print('You need to wait for a few minutes before accessing Twitter API again.')

    if ('statuses' in resp.json()):
        tweets_unfiltered += resp.json()['statuses']
        tweets = [tweet for tweet in tweets_unfiltered if ((tweet['retweeted'] != True) and ('RT @' not in tweet['text']))]

        # find the max_id_str for the next batch
        ids = [tweet['id'] for tweet in tweets_unfiltered]
        max_id_str = str(min(ids))

        # loop until as many tweets as total_count is collected
        number_of_tweets = len(tweets)
        while number_of_tweets < total_count:

            resp = api.request('search/tweets', {'q': #INSERT YOUR CODE
                                             'count': #INSERT YOUR CODE
                                             'lang':'en',
                                             'result_type':  #INSERT YOUR CODE
                                             'max_id': max_id_str,
                                             'geocode': #INSERT YOUR CODE
                          )

            if ('statuses' in resp.json()):
                tweets_unfiltered += resp.json()['statuses']
                tweets = [tweet for tweet in tweets_unfiltered if ((tweet['retweeted'] != True) and ('RT @' not in tweet['text']))]

                ids = [tweet['id'] for tweet in tweets_unfiltered]
                max_id_str = str(min(ids))

                number_of_tweets = len(tweets)

            print("{} tweets are collected for keyword {}. Last tweet created at {}".format(number_of_tweets, 
                                                                                    keyword, 
                                                                                    tweets[number_of_tweets-1]['created_at']))
    return tweets

1 Ответ

1 голос
/ 09 мая 2020

В чем ваш вопрос или проблема? Я не нашел в вашем сообщении.

Пара предложений ... Удалите параметры lang и result_type из вашего запроса. Поскольку вы используете geocode, вы не должны ожидать очень много результатов, так как вряд ли кто-то включает местоположение, когда он пишет твит.

Кроме того, вместо использования параметра max_id вы можете посмотреть на TwitterPager класс, который позаботится об этом за вас. Вот пример: https://github.com/geduldig/TwitterAPI/blob/master/examples/page_tweets.py.

...