使用 Python Tweepy 搜索词交集和并集 [英] Search term intersection and union using Python Tweepy
本文介绍了使用 Python Tweepy 搜索词交集和并集的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想使用 Python Tweepy 获取包含love"和/或#hate"的推文.但是使用我当前的代码如下,它只返回第一个术语(即爱").我几天来一直在尝试调试和阅读 Tweepy/Twitter 文档,但无济于事.请指教.
I would like to get the tweets that either contain 'love' and/or '#hate' using Python Tweepy. But using my current code as below, it only returns the first term (i.e. 'love'). I have been trying for days to debug and read the Tweepy/Twitter documentation to no avail. Please advice.
import tweepy
import time
ckey = ""
csecret = ""
atoken = ""
asecret = ""
OAUTH_KEYS = {'consumer_key':ckey, 'consumer_secret':csecret,
'access_token_key':atoken, 'access_token_secret':asecret}
auth = tweepy.OAuthHandler(OAUTH_KEYS['consumer_key'], OAUTH_KEYS['consumer_secret'])
api = tweepy.API(auth)
for tweet in tweepy.Cursor(api.search, q=('love' or '#hate'), since='2014-09-15', until='2014-09-16').items(500):
try:
print "Tweet created:", tweet.created_at
print "Tweet:", tweet.text.encode('utf8')
counter += 1
except IOError:
time.sleep(60)
continue
推荐答案
从 twitter API 文档来看,'or' 应该大写并且应该在单引号内:
From looking at the twitter API documentation, the 'or' should be capitalized and should be inside the single quotes:
q = ('love OR #hate')
q = ('love OR #hate')
https://dev.twitter.com/rest/public/search
这篇关于使用 Python Tweepy 搜索词交集和并集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文