tweepy.Cursor returns the same users over and over
<p>I am trying to get all the search results in a list.</p>

<p>Here is the code:</p>

<pre><code>cursor = tweepy.Cursor(api.search_users,"foo")
count = 0
for u in cursor.items(30):
    count += 1
    print count, u.id_str
print count
</code></pre>

<p>Alas, item 1 is the same as 21, 2 is the same as 22 &amp;c:</p>

<pre><code>1 19081001
2 313527365
3 89528870
4 682463
5 2607583036
6 219840627
7 725883651280363520
8 371980318
9 860066587
10 4794574949
11 88633646
12 137482245
13 1447284511
14 15369494
15 171657474
16 442113112
17 6130932
18 2587755194
19 191338693
20 528804165
21 19081001
22 313527365
23 89528870
24 682463
25 2607583036
26 219840627
27 725883651280363520
28 371980318
29 860066587
30 4794574949
30
</code></pre>

<p>How do I get <em>all</em> the search results?</p>

<p>as requested:</p>

<pre><code>dir(cursor)
['__class__',
 '__delattr__',
 '__dict__',
 '__doc__',
 '__format__',
 '__getattribute__',
 '__hash__',
 '__init__',
 '__module__',
 '__new__',
 '__reduce__',
 '__reduce_ex__',
 '__repr__',
 '__setattr__',
 '__sizeof__',
 '__str__',
 '__subclasshook__',
 '__weakref__',
 'items',
 'iterator',
 'pages']
</code></pre>

<p>As per the <a href="http://docs.tweepy.org/en/v3.5.0/api.html#API.search_users" rel="nofollow">tweepy documentation</a>, you should not pass a number greater than 20. You're passing 30, that's why you get repeated ids after 20 id entries.</p>

<p>I hacked up a bit and came up with the below code which will get all the users which match the search query (here <code>foo</code>).</p>

<pre><code>def get_users():
    try:
        count = 0
        all_users = []
        for page in tweepy.Cursor(api.search_users,"foo").pages():
            #page[0] has the UserObj
            id_str = page[0].id_str
            scr_name = page[0].screen_name
            print(count, id_str, scr_name)
            count += 1
            all_users.append((id_str, scr_name))

    except tweepy.error.TweepError as twerr:
        print(" sleep because of error.. ")
        time.sleep(10)
</code></pre>

<p>Of course, this is a very crude implementation. Please write a proper sleeper function to not exceed the twitter rate limit.</p>

python twitter tweepy