Search code examples
pythonpython-3.xtwittertweepy

How to fix Tweepy error 'Sorry, that page does not exist.' in suggested_users()


I am using tweepy==3.6.0 and Python 3.6

auth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET)
auth.set_access_token(access_token,
                          access_token_secret)
api = tweepy.API(auth)

# categories = api.suggested_categories()
users = api.suggested_users(slug='science')

suggested_users() raises this error:

raise TweepError(error_msg, resp, api_code=api_error_code) tweepy.error.TweepError: [{'code': 34, 'message': 'Sorry, that page does not exist.'}]


Solution

  • Based on the Twitter API reference, GET users/suggestions/:slug is used to:

    Access the users in a given category of the Twitter suggested user list.

    So, when using api.suggested_users(), you cannot specify an arbitrary category like 'science'. You need to take the category slug from one of the suggested categories, which you can retrieve with api.suggested_categories().

    Here's a working example that lists the users of the 1st suggested category (with categories[0].slug):

    categories = api.suggested_categories(lang='en')
    # print names and slugs of suggested categories
    for cat in categories:
        print(cat.name,' - ',cat.slug)
    
    users = api.suggested_users(slug=categories[0].slug, lang='en')
    # print id and screen names of suggested users
    for user in users:
        print(user.id, ' - ', user.screen_name)