如何向 tweepy 模块添加位置过滤器 [英] How to add a location filter to tweepy module

查看:39
本文介绍了如何向 tweepy 模块添加位置过滤器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现以下代码非常有效,可以让我在 Python Shell 中查看标准 1% 的 twitter firehose:

I have found the following piece of code that works pretty well for letting me view in Python Shell the standard 1% of the twitter firehose:

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):
        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(track=['manchester united'])

如何添加过滤器以仅解析来自特定位置的推文?我见过有人将 GPS 添加到其他与 Twitter 相关的 Python 代码中,但我在 Tweepy 模块中找不到任何特定于 sapi 的内容.

How do I add a filter to only parse tweets from a certain location? Ive seen people adding GPS to other twitter related Python code but I cant find anything specific to sapi within the Tweepy module.

有什么想法吗?

谢谢

推荐答案

流式 API 不允许同时按位置和关键字过滤.

The streaming API doesn't allow to filter by location AND keyword simultaneously.

边界框不作为其他过滤器参数的过滤器.例如track=twitter&locations=-122.75,36.8,-121.75,37.8 将匹配任何包含术语 Twitter(甚至非地理推文)或来自旧金山地区.

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.

来源:https://dev.twitter.com/docs/streaming-apis/parameters#locations

您可以做的是向流式 API 询问关键字或定位的推文,然后通过查看每条推文来过滤应用中的结果流.

What you can do is ask the streaming API for keyword or located tweets and then filter the resulting stream in your app by looking into each tweet.

如果您按如下方式修改代码,您将捕获英国的推文,然后这些推文将被过滤以仅显示包含曼联"的推文

If you modify the code as follows you will capture tweets in United Kingdom, then those tweets get filtered to only show those that contain "manchester united"

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

这篇关于如何向 tweepy 模块添加位置过滤器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆