2

我想使用 Python Tweepy 获取包含“love”和/或“#hate”的推文。但是使用我当前的代码如下,它只返回第一个词(即“爱”)。几天来,我一直在尝试调试和阅读 Tweepy/Twitter 文档,但无济于事。请指教。

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
4

1 回答 1

5

通过查看 twitter API 文档,'or' 应该大写并且应该在单引号内:

q = ('爱或#恨')

https://dev.twitter.com/rest/public/search

于 2014-09-16T22:45:10.003 回答