如何增加 Tweepy 中用于 Twitter API 访问的搜索字词数量?
How to increase number of search terms in Tweepy for Twitter API access?
我发现我最多只能使用以下代码包含 20 多个搜索词,然后它给我错误说搜索词太多。有什么办法可以绕过它吗?
import tweepy
searchTerms = '1' or '2' or '3' # to say ... '99' or '100'
tweets = tweepy.Cursor(api.search, q=searchTerms, since=startSince, until=endUntil).items()
根据 Twitter docs,q
参数受到限制。
A UTF-8, URL-encoded search query of 500 characters maximum, including operators. Queries may additionally be limited by complexity.
如果您想构建复杂的搜索词逻辑,您可以使用 Streamer 和监听器。您实际上是在进行自己的过滤。这是一个简单的监听器示例。我试图给出一些从 on_status
方法返回的流行对象,作为 json
.
返回
import json
class SListener(StreamListener):
def __init__(self, api = None, fprefix = 'streamer'):
self.api = api or API()
self.counter = 0
self.fprefix = fprefix
def on_data(self, data):
elif 'limit' in data:
if self.on_limit(json.loads(data)['limit']['track']) is False:
return False
elif 'warning' in data:
warning = json.loads(data)['warnings']
print warning['message']
return false
def on_status(self, status):
status_obj = json.loads(status)
username = status_obj["user"]["screen_name"]
userID = status_obj["user"]["id"]
user_loc = status_obj["user"]["location"]
tweet_date_time = status_obj["created_at"]
tweetID = status_obj["id"]
tweet = status_obj["text"].encode('utf-8')
searchTerms = ['1','2','3'] # to say ... '99' or '100'
if any(query in tweet for query in searchTerms):
print(tweet) #or do something with it
我发现我最多只能使用以下代码包含 20 多个搜索词,然后它给我错误说搜索词太多。有什么办法可以绕过它吗?
import tweepy
searchTerms = '1' or '2' or '3' # to say ... '99' or '100'
tweets = tweepy.Cursor(api.search, q=searchTerms, since=startSince, until=endUntil).items()
根据 Twitter docs,q
参数受到限制。
A UTF-8, URL-encoded search query of 500 characters maximum, including operators. Queries may additionally be limited by complexity.
如果您想构建复杂的搜索词逻辑,您可以使用 Streamer 和监听器。您实际上是在进行自己的过滤。这是一个简单的监听器示例。我试图给出一些从 on_status
方法返回的流行对象,作为 json
.
import json
class SListener(StreamListener):
def __init__(self, api = None, fprefix = 'streamer'):
self.api = api or API()
self.counter = 0
self.fprefix = fprefix
def on_data(self, data):
elif 'limit' in data:
if self.on_limit(json.loads(data)['limit']['track']) is False:
return False
elif 'warning' in data:
warning = json.loads(data)['warnings']
print warning['message']
return false
def on_status(self, status):
status_obj = json.loads(status)
username = status_obj["user"]["screen_name"]
userID = status_obj["user"]["id"]
user_loc = status_obj["user"]["location"]
tweet_date_time = status_obj["created_at"]
tweetID = status_obj["id"]
tweet = status_obj["text"].encode('utf-8')
searchTerms = ['1','2','3'] # to say ... '99' or '100'
if any(query in tweet for query in searchTerms):
print(tweet) #or do something with it