【发布时间】:2021-05-17 10:33:36
【问题描述】:
我正在使用 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(在 cmets 中请求)
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
【问题讨论】:
标签: python api twitter scripting tweepy