【问题标题】:How can I return sentiment value from Tweepy class and Pass it to Giphy API如何从 Tweepy 类返回情绪值并将其传递给 Giphy API
【发布时间】:2017-05-13 21:26:58
【问题描述】:

我正在使用 tweepy API 并希望返回找到的数据,将其传递给 Giphy API 函数并将其用作 giphy 搜索的查询。

class TweetStreamListener(StreamListener):
     # on success
    def on_data(self, data):
     # decode json
        dict_data = json.loads(data)
     # pass tweet into TextBlob
        tweet = TextBlob(dict_data["text"])
     # output sentiment polarity
        print (tweet.sentiment.polarity)
     # determine if sentiment is positive, negative, or neutral
        if tweet.sentiment.polarity < 0:
            sentiment = "negative"
        elif tweet.sentiment.polarity == 0:
            sentiment = "neutral"
        else:
            sentiment = "positive"
    # output sentiment
        print (sentiment)
        return(sentiment)

上面的情绪值是我想从类中返回并传递给 GIPHY API 函数

# Giphy API Functions
def setup():
   url = api + apiKey + query
   print("Getting data with query 'sentiment'")
   json.loads(url, my_callback)
def my_callback(data):
  print("Data recieved!")
  print(data.data[0].images.original.url)

我正在考虑为 giphy 函数创建一个新文件,但我可以将它们添加到这个文件中吗?

注意:我认为以下行会返回情绪值

    query = "&q=" + str(TweetStreamListener.on_data)
    print(query)

注意:相反,我得到了这个,“&q=function TweetStreamListener.on_data at 0x1065c3268”

【问题讨论】:

    标签: python api tweepy data-science giphy


    【解决方案1】:

    on_data 方法接受一个参数,但您尝试打印方法本身而不是结果。我不熟悉 TweetStream api,但你应该看看这个:https://www.dataquest.io/blog/streaming-data-python/

    【讨论】:

    • 好帖子,谢谢!我认为您是在暗示我创建了一个数据库来存储推文。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-02-13
    • 1970-01-01
    • 1970-01-01
    • 2017-05-23
    • 1970-01-01
    • 1970-01-01
    • 2020-02-02
    相关资源
    最近更新 更多