Error scraping friend list from Twitter user w/ api.lookup_users: Too many terms specified in query
<p>The code below was provided to another user who was scraping the "friends" (not followers) list of a specific Twitter user. For some reason, I get an error when using "api.lookup_users". The error states "Too many terms specified in query". Ideally, I would like to scrape the followers and output a csv with the screen names (not ids). I would like their descriptions as well, but can do this in a separate step unless there is a suggestion for pulling both pieces of information. Below is the code that I am using:</p>

<pre><code>import time
import tweepy
import csv

#Twitter API credentials
consumer_key = ""
consumer_secret = ""
access_key = ""
access_secret = ""

auth = tweepy.auth.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_key, access_secret)
api = tweepy.API(auth)

ids = []
for page in tweepy.Cursor(api.friends, screen_name="").pages():
    ids.extend(page)
    time.sleep(60)

print(len(ids))

users = api.lookup_users(user_ids=ids) #iterates through the list of         users and prints them
for u in users:
    print(u.screen_name) 
</code></pre>

<p>From the error you get, it seems that you are putting too many ids at once in the api.lookup_users request. Try splitting you list of ids in smaller parts and make a request for each part. </p>

<pre><code>import time
import tweepy
import csv

#Twitter API credentials
consumer_key = ""
consumer_secret = ""
access_key = ""
access_secret = ""

auth = tweepy.auth.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_key, access_secret)
api = tweepy.API(auth)

ids = []
for page in tweepy.Cursor(api.friends, screen_name="").pages():
    ids.extend(page)
    time.sleep(60)

print(len(ids))

idChunks = [ids[i:i + 300] for i in range(0, len(ids), 300)]
users = []
for idChunk in idChunks:
   try:
       users.append(api.lookup_users(user_ids=idChunk))
   except tweepy.error.RateLimitError:
       print("RATE EXCEDED. SLEEPING FOR 16 MINUTES")
       time.sleep(16*60)
       users.append(api.lookup_users(user_ids=idChunk))

for u in users:
    print(u.screen_name)
    print(u.description)
</code></pre>

<p>This code has not been tested, and does not writes the CSV, but it should help you getting past that error you were having. The size of 300 for the chunks is completely arbitrary, adjust it if it is too small (or too big).</p>

python tweepy