如何使用位置和关键字过滤推文? [英] How do I filter tweets using location AND keyword?

查看:28
本文介绍了如何使用位置和关键字过滤推文?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是一个新的Python用户,一直在试验tweepy。我知道Twitter API不允许同时对位置和关键字进行过滤。为了解决这个问题,我修改了下面的代码:How to add a location filter to tweepy module。虽然它在只有几个关键字的情况下工作得很好,但当我增加关键字的数量时,它会停止打印状态。我认为这可能是因为迭代关键字列表不是最好的方法。有没有人有什么建议来解决这个问题?

import sys
import tweepy
import json

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)
### keywords for the public stream
keyword = "iPhone", "Samsung", "HTC", "Sony", "Blackberry"
### initialize blank list to contain tweets
tweets = []
### file name that you want to open is the second argument
f = open('today.txt', 'a')

class CustomStreamListener(tweepy.StreamListener):
    global tweets
    def on_status(self, status):
        ### info that you want to capture
        info = status.id, status.text, status.created_at, status.place, status.user, status.in_reply_to_screen_name, status.in_reply_to_status_id 
        for word in keyword:
            if word in status.text.lower():
                print status.text
                # this is for writing the tweets into the txt file
                f.write(str(info))
                try:
                    tweets.append(info)
                except:
                    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

### filter for location
# locations should be a pair of longtitude and latitude pairs, with the southwest corner
# of the bounding box coming first
sapi = tweepy.streaming.Stream(auth, CustomStreamListener())    
sapi.filter(locations=[103.60998,1.25752,104.03295,1.44973])

推荐答案

使用正则表达式搜索推文。如下所示

    import re
    keyword = ["iPhone", "Samsung", "HTC", "Sony", "Blackberry"]
    patterns = [r'%s' % re.escape(s.strip()) for s in keyword.lower()]
    there = re.compile('|'.join(patterns))
    stream=["i have a iPhone","i dont like Samsung","HTC design are awesome","Sony camera is good","Blackberry lost market","Nokia soldout to windows"]
    for i in stream:
        if there.search(i):
            print("Tweet Found  %r" % (i))

这篇关于如何使用位置和关键字过滤推文?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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