python查看坐标是否在范围内_if语句检查坐标是否在范围内

感谢@Alfe和@Hellsgate提供的帮助和建议。在

下面的代码可以工作(目前Hellsgate上面的代码返回带有地理标记的tweets,但不按坐标过滤)

在CustomStreamListener之上,您只需要添加import语句和OAuth方法。下面的代码只会返回来自英国各地搜索词“稀有钻石,苹果酱,不和谐”的推文class CustomStreamListener(tweepy.StreamListener):

def check_coords(self, status):

if status.coordinates is not None:

lat = status.coordinates['coordinates'][1]

lng = status.coordinates['coordinates'][0]

return 49.7 < lat < 58.5 and -6 < lng < 2.1

def on_status(self, status):

if self.check_coords(status):

# if check_coords returns true, the following will run

try:

print "'tweet text'%s\n\ 'User Name't%s\n\ 'Created at't%s\n\ 'Created with't%s\n\ 'geo't%s\ 'coordinates't%s" % (status.text,

status.author.screen_name,

status.created_at,

status.source,

status.geo,

status.coordinates)

except Exception, e:

print >> sys.stderr, 'Encountered Exception:', e

pass

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

streaming_api = tweepy.streaming.Stream(auth, CustomStreamListener(), timeout=60)

print >> sys.stderr, 'Filtering the public timeline for "%s"' % (' '.join(sys.argv[1:]),)

"""For track=[""], put in words to search for. Commas separate individual terms IE"Applejack, Discord",

to search for tweets containing more than one term, separate keywords with a space. IE "Rarity Diamonds" """

streaming_api.filter(follow=None, track=["Rarity Diamonds,Applejack,Discord"])

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值