我是一个新的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])
使用正则表达式搜索tweet。具体如下:
相关问题 更多 >
编程相关推荐