使用Python Tweepy搜索术语交集和并集 [英] Search term intersection and union using Python Tweepy

查看:250
本文介绍了使用Python Tweepy搜索术语交集和并集的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用Python Tweepy获得包含"love"和/或"#hate"的推文.但是使用下面的当前代码,它只会返回第一项(即"love").我一直在尝试调试和阅读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屋!

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