I'm trying to get tweets from a specific location using Tweepy but I get this error when I run the code
raise TweepError("Wrong number of locations points, "
tweepy.error.TweepError: Wrong number of locations points, it has to be a multiple of 4
In my code, I try to get tweets from New York City with the location coordinates for NY. How can I get tweets from NY alone? My guess is to use a range of coordinates say between x,y and y,z. How do I go about this?
Here is my code:
class StdOutListener(StreamListener):
""" A listener handles tweets are the received from the stream.
This is a basic listener that just prints received tweets to stdout.
"""
def on_data(self, data):
try:
print(data)
saveFile = open('newtweets.csv', 'a')
saveFile.write(data)
saveFile.write('/n').encode("utf-8")
saveFile.close()
return True
except BaseException:
print ('failed ondata')
time.sleep(5)
def on_error(self, status):
print(status.encode("utf-8"))
if __name__ == '__main__':
l = StdOutListener()
auth = OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
#ASK FOR KEYWORD TO COLLECT DATA
user_keyword=input("What keyword do you want to mine?")
stream = Stream(auth, l)
stream.filter(locations=[40.7127,74.0059], track=[user_keyword])
In order to get the location we have to do the following : Identify the user ID or the screen name of the profile. Get the User object of the profile using the get_user() method with the user ID or the screen name. From this object, fetch the location attribute present in it.
Use the "near:" and "within:" operators to find Tweets within a certain distance of a place. For example, if you wanted to find marketers within 10 miles of Manhattan, you'd put marketers near:Manhattan within:10mi in Twitter's search box.
Scroll to any Tweet in the list. If the user has signed up for the geolocation feature, you'll find the location information displayed in a red font under the main message body and above the time and date line at the bottom of the post.
Steps to obtain keys: – For access token, click ” Create my access token”. The page will refresh and generate access token. Tweepy is one of the library that should be installed using pip. Now in order to authorize our app to access Twitter on our behalf, we need to use the OAuth Interface.
It needs 4 coordinates. NYC, for example:
stream.filter(locations=[-74.1687,40.5722,-73.8062,40.9467])
Here's the first google result of a site that let's you draw bounding boxes. Select CSV format on the bottom-left of the page.
It's important to note, as mentioned in this post, you cannot filter by both location AND keyword.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With