【问题标题】:tweepy.Cursor returns the same users over and overtweepy.Cursor 一遍又一遍地返回相同的用户
【发布时间】:2017-02-15 19:01:13
【问题描述】:

我正在尝试将所有搜索结果放在一个列表中。

代码如下:

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']

【问题讨论】:

  • 试试dir(cursor) 看看你能用它做什么。如果你没有发现任何令人惊奇的东西,它必须是一个 pythonic 解决方案。
  • 我将它添加到问题文本中,但我看不出这与 repeated 输出有何关联。
  • 你想达到什么目的?您能否在问题中清楚地添加要求!
  • @kmario23:我正在尝试将所有搜索结果放在一个列表中。我不认为这是相关的。我不应该看到重复的条目。
  • 我发现了这个问题。你能试试我的答案吗?

标签: python twitter tweepy


【解决方案1】:

根据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)

当然,这是一个非常粗略的实现。请编写适当的 sleeper 函数以不超过 twitter 速率限制。

【讨论】:

  • 我仍然得到(0, u'19081001', u'foofighters') 打印两次。
  • @sds 是的,我也注意到了。但这是一个奇怪的错误,我不知道为什么。除此之外,没有更多重复的条目被打印出来。我用更多的关键字检查了它。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-07-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-01-15
  • 1970-01-01
相关资源
最近更新 更多