gpt4 book ai didi

python - tweepy.Cursor 一遍又一遍地返回相同的用户

转载 作者:太空宇宙 更新时间:2023-11-03 16:05:12 26 4
gpt4 key购买 nike

我正在尝试获取列表中的所有搜索结果。

这是代码:

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

唉,第 1 项与 21 相同,第 2 项与 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

如何获取所有搜索结果?

根据要求:

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

最佳答案

根据 tweepy documentation ,您不应传递大于 20 的数字。您传递的是 30,这就是为什么在 20 个 id 条目后会得到重复的 id。

我做了一些修改,想出了下面的代码,它将获取与搜索查询匹配的所有用户(此处为 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)

当然,这是一个非常粗糙的实现。请编写适当的 sleep 函数,以免超过 Twitter 速率限制。

关于python - tweepy.Cursor 一遍又一遍地返回相同的用户,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39906052/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com