Nat Tomov
Nat Tomov

Reputation: 21

How do I download text of Tweets using the API?

I am trying to search Twitter for tweets containing certain keywords, and then download a dataset of all the tweets from the past two weeks that contain these keywords. The dataset should contain the text of the tweet, in addition to any links attached.

What is the procedure to do this with the Twitter API? I have a developer account.

Upvotes: 2

Views: 279

Answers (1)

Jonas
Jonas

Reputation: 4058

With Python you can use a library like TwitterAPI. Here is a simple example to get you started.

from TwitterAPI import TwitterAPI

SEARCH_TERM = 'pizza'

api = TwitterAPI(<consumer key>, <consumer secret>, <access token key>, <access token secret>)

r = api.request('search/tweets', {'q': SEARCH_TERM})

for item in r:
    print(item['text'] if 'text' in item else item)

This will return tweets from the last week. To get older tweets you must first request access to Twitter's Premium Search. Then, you can use the code example here to get you started with Premium Search.

Upvotes: 1

Related Questions