我试图使用Twython从Python上的twitter查询中提取推文

时间:2014-05-19 15:23:22

标签: python twitter twython

我正在尝试查看与特定搜索字词相关的推文列表,并尝试提取所有主题标签。我希望制作一个包含所有主题标签的python列表。我开始使用Twython如下

from twython import Twython

api_key = 'xxxx'
api_secret = 'xxxx'
acces_token = 'xxxx'
ak_secret = 'xxxx'


t =  Twython(app_key = api_key, app_secret = api_secret, oauth_token = acces_token,          oauth_token_secret = ak_secret)
search = t.search(q = 'Python', count = 10)

tweets = search['statuses']
hashtags = []
for tweet in tweets:
    b = (tweet['text'],"\n")
    if b.startswith('#'):
        hastags.append(b)

它似乎不起作用。我得到的错误是 '元组对象没有属性startswith'

我不确定我是否打算首先列出所有状态并使用上述方法提取。或者可以先进行状态列表。

谢谢

1 个答案:

答案 0 :(得分:1)

这是正确的,字符串具有startswith属性而元组则没有。

将最后三行更改为:

b = (tweet['text'])
if b.startswith("#") is True:
    hashtags.append(b)

如果你真的想要换行,那就是:

b = (tweet['text'] + "\n")
if b.startswith("#") is True:
    hashtags.append(b)