0

I am writing a function that filter the keyword of my timeline using python-twitter. I do not know how to filter tweets in my timeline. should I use GetSearch(term=keyword)?

import twitter
api = twitter.Api(my keys)

timelines = api.GetHomeTimeline()
for lines in timelines:
    print(lines.text)
Sean
  • 1
  • `[lines.text for lines in timelines if 'your keyword' in lines.text]`, should work, otherwise I'm not sure if the api has a specific method for filtering by keyword – chickity china chinese chicken Nov 20 '18 at 01:06
  • or if you want to keep the expanded `for`-loop syntax: `for lines in timelines: if 'your keyword' in lines.text: print(lines.text)` – chickity china chinese chicken Nov 20 '18 at 01:19
  • could also use python's [`filter()`](https://docs.python.org/3/library/functions.html#filter) function for each Status in `GetHomeTimeline` list: `print(list(filter(lambda k: 'your keyword' in k,[k.text for k in timelines])))` will return a list of tweets containing the "keyword" – chickity china chinese chicken Nov 20 '18 at 01:27

0 Answers0