Babatunde_Onikoyi
Babatunde_Onikoyi

Reputation: 11

How do I filter tweets in python using Tweepy?

Below is my code where I have hashed out all the different methods I have tried to specify that it should only return tweets based on a certain location but all do not work.

Anyone has any ideas?

wb_ehu = Workbook()

search_words = "Blue Bag"
new_search = search_words + " -filter:retweets"
date_since = "2020-06-01"

ws_ehu = wb_ehu.active
ws_ehu["A1"] = "Created_Date"
ws_ehu["B1"] = "Tweet"
ws_ehu["C1"] = "Username"
ws_ehu["D1"] = "Location"

for tweet in tweepy.Cursor(api.search, q=new_search, count=1000, tweet_mode="extended",
                       lang="en",
                       #Place= "London,UK"
                       #geo = [-0.127758, 51.507351]
                       #geo = [51.507351,-0.127758]
                       #geocode = [-0.127758, 51.507351]
                       #geocode = [51.507351,-0.127758]
                       #coordinates = [-0.127758, 51.507351]
                       #coordinates = [51.507351,-0.127758]
                       since=date_since).items():
    ws_ehu.append([tweet.created_at, tweet.full_text, tweet.user.screen_name, tweet.user.location ])

Also, I tried to specify getting tweets from a specific date but it never returns any tweet more than the current month.

Upvotes: 0

Views: 673

Answers (1)

Babatunde_Onikoyi
Babatunde_Onikoyi

Reputation: 11

I have been able to figure it out using datetime and if statement with geocordinate.

import datetime

wb_ehu = Workbook()
search_words = "Blue Bag"
new_search = search_words + " -filter:retweets"
startDate = datetime.datetime(2021, 6, 1, 0, 0, 0)
endDate =   datetime.datetime(2021, 7, 3, 0, 0, 0)

ws_ehu = wb_ehu.active
ws_ehu["A1"] = "Created_Date"
ws_ehu["B1"] = "Tweet"
ws_ehu["C1"] = "Username"
ws_ehu["D1"] = "Location"

for tweet in tweepy.Cursor(api.search, 
                       geocode= "51.507351,-0.127758,50mi",
                       q=new_search, count=1000, tweet_mode="extended",
                       lang="en",
                       ).items():
 if tweet.created_at < endDate and tweet.created_at > startDate:
    ws_ehu.append([tweet.created_at, tweet.full_text, 
                   tweet.user.screen_name, tweet.user.location ])

Upvotes: 1

Related Questions