View the step-by-step solution to:

How to run the script for example, print_tweets.py "Martha Stewart" to extract and print a sample of tweets about Martha Stewart?

How to run the script for example, print_tweets.py "Martha Stewart" to extract and print a sample of tweets about Martha Stewart? And below is the part of the script that using for downloading tweets.


#

consumer_key = "YOUR-KEY"

consumer_secret = "YOUR-KEY"

access_token = "YOUR-KEY"

access_token_secret = "YOUR-KEY"

auth = tweepy.OAuthHandler(consumer_key, consumer_secret)

auth.set_access_token(access_token, access_token_secret)

api = tweepy.API(auth)

#

input_queries = ['MarthaStewart ']

download_tweet_count = 10000 # number of tweets we want to collect

tweetsPerQry = 100

sinceId = None

max_id = -1L


dataset = defaultdict(list)

for input_query in input_queries:

print(input_query)

counter = 0

while counter < download_tweet_count:

try:

if (max_id <= 0):

if (not sinceId):

new_tweets = api.search(q=input_queries, count=tweetsPerQry)

else:

new_tweets = api.search(q=input_queries, count=tweetsPerQry, since_id=sinceId)

else:

if (not sinceId):

new_tweets = api.search(q=input_queries, count=tweetsPerQry, max_id=str(max_id - 1))

else:

new_tweets = api.search(q=input_queries, count=tweetsPerQry, max_id=str(max_id - 1), since_id=sinceId)

for tweet in new_tweets:

dataset['topic'].append(input_query)

dataset['id'].append(tweet.id)

dataset['username'].append(tweet.author.screen_name)

dataset['name'].append(tweet.author.name)

dataset['user_followers_count'].append(tweet.author.followers_count)

dataset['user_friends_count'].append(tweet.author.friends_count)

dataset['text'].append(tweet.text)

dataset['created_at'].append(tweet.created_at.strftime("%Y-%m-%d %H:%M:%S"))

dataset['favorite_count'].append(tweet.favorite_count)

dataset['retweet_count'].append(tweet.retweet_count)

dataset['hashtags'].append(','.join([ht['text'] for ht in tweet.entities['hashtags']]))

dataset['mentioned_urls'].append(','.join([url['url'] for url in tweet.entities['urls']]))

dataset['mentioned_user_ids'].append(','.join([mention['id_str'] for mention in tweet.entities['user_mentions']]))

dataset['mentioned_user_names'].append(','.join([mention['screen_name'] for mention in tweet.entities['user_mentions']]))

counter +=len(new_tweets)

max_id = new_tweets[-1].id

if counter == download_tweet_count: break

except TweepError:

print(len(dataset['id']))

print('Wait 15 min')

# wait 15 min

time.sleep(15*60)

Recently Asked Questions

Why Join Course Hero?

Course Hero has all the homework and study help you need to succeed! We’ve got course-specific notes, study guides, and practice tests along with expert tutors.

-

Educational Resources
  • -

    Study Documents

    Find the best study resources around, tagged to your specific courses. Share your own to gain free Course Hero access.

    Browse Documents
  • -

    Question & Answers

    Get one-on-one homework help from our expert tutors—available online 24/7. Ask your own questions or browse existing Q&A threads. Satisfaction guaranteed!

    Ask a Question