TypeError:序列项5:应为str实例,未找到非类型

2024-10-02 08:18:48 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在使用Tweepy从Twitter上传输推文。目前,我正在尝试实现一个代码,该代码将在数据库中更新用户ID列表时断开流,并使用更新的列表打开一个新流

问题:

该实现工作正常,但不知何故,.filter()认为我传递的是None,而不是列表。在将列表传递给方法之前,我可以打印列表。那么我错过了什么

更多上下文:

下面是错误的来源:line 459, in filter self.body['follow'] = u','.join(follow).encode(encoding)

有些东西评估为零,我不知道它是什么。有人救我

代码:

def get_user_accounts():
    global last_number_of_accounts

    try:
        # All psycopg2 and python mumbo jumbo that gives me back a list of twitter_user_ids
        # That list is assigned to `new_user_ids`

        number_of_accounts = len(new_user_ids)

        if last_number_of_accounts == -1:
            return {"has_changed": True, "user_ids": new_user_ids, "first_connection": True}

        if number_of_accounts != last_number_of_accounts:
            return {"has_changed": True, "user_ids": new_user_ids, "first_connection": False}
        else:
            return {"has_changed": False, "user_ids": new_user_ids, "first_connection": False}
    except Exception as e:
        print(e)


def check_number_of_accounts():
    threading.Timer(3, check_number_of_accounts).start()
    global last_number_of_accounts
    global streamUserTweets
    new_user_ids = get_user_accounts()

    if new_user_ids['has_changed']:
        print(new_user_ids['user_ids'])  # This does not print None
        last_number_of_accounts = len(new_user_ids['user_ids'])

        if new_user_ids['first_connection']:
            # Open stream for the first time
            try:
                streamUserTweets.filter(follow=new_user_ids['user_ids'])  # So how is this none!!!
            except Exception as e:
                print(e)
        else:
            # Disconnect stream first, then connect with updated list
            try:
                streamUserTweets.disconnect()
                streamUserTweets.filter(follow=new_user_ids['user_ids'])
            except Exception as e:
                print(e)

# TWITTER AUTHENTICATION STUFF

userTweetStreamListener = UserTweetStreamListener()
streamUserTweets = tweepy.Stream(auth=api.auth, listener=userTweetStreamListener)

last_number_of_accounts = -1

check_number_of_accounts()

整个回溯(在评论中要求)

Traceback (most recent call last):
  File "path/to/file.py", line 247, in <module>
    check_number_of_accounts()
  File "path/to/file.py", line 208, in check_number_of_accounts
    streamUserTweets.filter(follow=new_user_ids['user_ids'])
  File "/path/to/env/path/to/tweepy/streaming.py", line 459, in filter
    self.body['follow'] = u','.join(follow).encode(encoding)
TypeError: sequence item 5: expected str instance, NoneType found

Tags: oftoidsnumber列表newcheckfilter
1条回答
网友
1楼 · 发布于 2024-10-02 08:18:48

它并不是说你通过了None

TypeError: sequence item 5: expected str instance, NoneType found

它表示您要传递的列表/序列中的第6个元素(item 5)是None.join()只能连接字符串,这会打乱方法。print(new_user_ids['user_ids'])行的输出应该可以帮助您找到问题所在

相关问题 更多 >

    热门问题