<p>流式处理API不允许同时按位置和关键字筛选。</p>
<blockquote>
<p>Bounding boxes do not act as filters for other filter parameters. For example
track=twitter&locations=-122.75,36.8,-121.75,37.8 would match any tweets containing
the term Twitter (even non-geo tweets) OR coming from the San Francisco area.</p>
</blockquote>
<p>来源:<a href="https://dev.twitter.com/docs/streaming-apis/parameters#locations">https://dev.twitter.com/docs/streaming-apis/parameters#locations</a></p>
<p>你所能做的就是向流媒体API请求关键字或定位的tweets,然后通过查看每个tweet来过滤应用程序中的结果流。</p>
<p>如果您修改代码如下,您将捕获英国的tweets,那么这些tweets将被过滤,只显示包含“manchester United”的tweets</p>
<pre><code>import sys
import tweepy
consumer_key=""
consumer_secret=""
access_key=""
access_secret=""
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_key, access_secret)
api = tweepy.API(auth)
class CustomStreamListener(tweepy.StreamListener):
def on_status(self, status):
if 'manchester united' in status.text.lower():
print status.text
def on_error(self, status_code):
print >> sys.stderr, 'Encountered error with status code:', status_code
return True # Don't kill the stream
def on_timeout(self):
print >> sys.stderr, 'Timeout...'
return True # Don't kill the stream
sapi = tweepy.streaming.Stream(auth, CustomStreamListener())
sapi.filter(locations=[-6.38,49.87,1.77,55.81])
</code></pre>