【问题标题】:extracting trending hashtags via Twitter API通过 Twitter API 提取趋势标签
【发布时间】:2014-06-06 23:37:17
【问题描述】:

我正在使用以下代码来运行一个 twitter 机器人,该机器人应该使用在发推文时处于趋势顶部的主题标签,并且该机器人使用它来发布著名哲学家的思想。

#!/usr/bin/env python
# -*- coding: utf-8 -*-

import tweepy, time, sys 

argfile = str(sys.argv[1])

#enter the corresponding information from your Twitter application:
CONSUMER_KEY = 'secret'
CONSUMER_SECRET = 'secret'
ACCESS_KEY = 'secret'
ACCESS_SECRET = 'secret'
auth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET)
auth.set_access_token(ACCESS_KEY, ACCESS_SECRET)
api = tweepy.API(auth)

filename=open(argfile,'r')
f=filename.readlines()
filename.close()

trends1 = api.trends_place(1)
print trends1
hashtags = [x['name'] for x in trends1[0]['trends'] if x['name'].startswith('#')]
# print hashtags
print hashtags[0]
trend_hashtag = hashtags[0]

# Tweet every X min ...
for line in f:
    api.update_status(line + ' ' + trend_hashtag)
    time.sleep(1800)

问题在于代码永久存储了它在第一次迭代中检索到的趋势标签。如何实现为每条推文重新提取主题标签(在这种情况下,每 1800 秒)?

提前致谢!

【问题讨论】:

    标签: python twitter hashtag tweepy


    【解决方案1】:

    明显的变化是更新循环内的主题标签

    for line in f:
        trends1 = api.trends_place(1)
        print trends1
        hashtags = [x['name'] for x in trends1[0]['trends'] if x['name'].startswith('#')]
        # print hashtags
        print hashtags[0]
        trend_hashtag = hashtags[0]
        api.update_status("{0} {1}".format(line, trend_hashtag)) # more modern format
        time.sleep(1800)
    

    【讨论】:

    • 哦,这看起来很简单,试试 tnx!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-03-29
    • 2016-12-16
    • 2014-09-17
    • 2017-03-11
    • 1970-01-01
    • 1970-01-01
    • 2011-03-11
    相关资源
    最近更新 更多