【问题标题】:Search term intersection and union using Python Tweepy使用 Python Tweepy 搜索词交集和并集
【发布时间】:2014-11-10 18:12:47
【问题描述】:

我想使用 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

【问题讨论】:

    标签: python-2.7 twitter tweepy social-media python-twitter


    【解决方案1】:

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

    q = ('爱或#恨')

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

    【讨论】:

    • 我确实查看了 Twitter 文档,并且我尝试了 q = ("love" OR "#hate") 并且没有工作并且认为它不相关。事实证明,正如您所建议的,它需要在一个字符串参数中。谢谢。
    猜你喜欢
    • 1970-01-01
    • 2018-06-29
    • 2013-10-28
    • 2011-04-18
    • 1970-01-01
    • 2015-05-06
    • 2017-02-18
    • 2021-08-25
    • 2015-08-14
    相关资源
    最近更新 更多