Reputation: 60084
I am trying to get all the search results in a list.
Here is the 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
Alas, item 1 is the same as 21, 2 is the same as 22 &c:
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
How do I get all the search results?
as requested:
dir(cursor)
['__class__',
'__delattr__',
'__dict__',
'__doc__',
'__format__',
'__getattribute__',
'__hash__',
'__init__',
'__module__',
'__new__',
'__reduce__',
'__reduce_ex__',
'__repr__',
'__setattr__',
'__sizeof__',
'__str__',
'__subclasshook__',
'__weakref__',
'items',
'iterator',
'pages']
Upvotes: 2
Views: 1315
Reputation: 61505
As per the tweepy documentation, you should not pass a number greater than 20. You're passing 30, that's why you get repeated ids after 20 id entries.
I hacked up a bit and came up with the below code which will get all the users which match the search query (here foo
).
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)
Of course, this is a very crude implementation. Please write a proper sleeper function to not exceed the twitter rate limit.
Upvotes: 1