How do I filter tweets using location AND keyword?(如何使用位置和关键字过滤推文?)
本文介绍了如何使用位置和关键字过滤推文?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我是一个新的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))
这篇关于如何使用位置和关键字过滤推文?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
沃梦达教程
本文标题为:如何使用位置和关键字过滤推文?


基础教程推荐
猜你喜欢
- 如何在Python中绘制多元函数? 2022-01-01
- Python 的 List 是如何实现的? 2022-01-01
- 使 Python 脚本在 Windows 上运行而不指定“.py";延期 2022-01-01
- 使用Python匹配Stata加权xtil命令的确定方法? 2022-01-01
- 使用 Google App Engine (Python) 将文件上传到 Google Cloud Storage 2022-01-01
- 合并具有多索引的两个数据帧 2022-01-01
- 症状类型错误:无法确定关系的真值 2022-01-01
- 如何在 Python 中检测文件是否为二进制(非文本)文 2022-01-01
- 哪些 Python 包提供独立的事件系统? 2022-01-01
- 将 YAML 文件转换为 python dict 2022-01-01