SlideShare a Scribd company logo
Write a Python program that extracts 1000 unique links from Twitter using Tweepy. Desired
output only has screen name and extended urls.
Solution
import tweepy /tweepy/tweepy
import tweepy /tweepy/tweepyimport csv#Twitter API credentialsconsumer_key =
""consumer_secret = ""access_key = ""access_secret = ""def
get_all_tweets(screen_name):#Twitter only allows access to a users most recent 3240 tweets
with this method#authorize twitter, initialize tweepyauth =
tweepy.OAuthHandler(consumer_key, consumer_secret)auth.set_access_token(access_key,
access_secret)api = tweepy.API(auth)#initialize a list to hold all the tweepy Tweetsalltweets =
[]#make initial request for most recent tweets (200 is the maximum allowed count)new_tweets =
api.user_timeline(screen_name = screen_name,count=200)#save most recent
tweetsalltweets.extend(new_tweets)#save the id of the oldest tweet less oneoldest = alltweets[-
1].id - 1#keep grabbing tweets until there are no tweets left to grabwhile len(new_tweets) >
0:print "getting tweets before %s" % (oldest)#all subsiquent requests use the max_id param to
prevent duplicatesnew_tweets = api.user_timeline(screen_name =
screen_name,count=200,max_id=oldest)#save most recent
tweetsalltweets.extend(new_tweets)#update the id of the oldest tweet less oneoldest =
alltweets[-1].id - 1print "...%s tweets downloaded so far" % (len(alltweets))#transform the
tweepy tweets into a 2D array that will populate the csvouttweets = [[tweet.id_str,
tweet.created_at, tweet.text.encode("utf-8")] for tweet in alltweets]#write the csvwith
open('%s_tweets.csv' % screen_name, 'wb') as f:writer = csv.writer(f)
writer.writerow(["id","created_at","text"])writer.writerows(outtweets)passif __name__ ==
'__main__':#pass in the username of the account you want to
downloadget_all_tweets("J_tsar")

More Related Content

PDF
Build a Twitter Bot with Basic Python
PDF
Twitter4R OAuth
PDF
Building TweetEngine
PDF
Twitter sentiment analysis for cryptoassets
PDF
Can someone help with these functions doAddTweet doEditTwe.pdf
PPTX
Development of Twitter Application #4 - Timeline and Tweet
PDF
Collect twitter data using python
PDF
Collect twitter data using python
Build a Twitter Bot with Basic Python
Twitter4R OAuth
Building TweetEngine
Twitter sentiment analysis for cryptoassets
Can someone help with these functions doAddTweet doEditTwe.pdf
Development of Twitter Application #4 - Timeline and Tweet
Collect twitter data using python
Collect twitter data using python

Similar to Write a Python program that extracts 1000 unique links from Twitter .pdf (20)

PPTX
How to get data from twitter (by hnnrrhm)
ODT
Streaming twitter data using kafka
PDF
PyLecture2 -NetworkX-
KEY
TwitterKitではじめる OAuthスピードクッキング
PPT
Social media analysis in R using twitter API
PDF
Tips and tricks for building api heavy ruby on rails applications
PDF
Api Doc
PDF
Api Doc
PPTX
Development of Twitter Application #3 - OAuth
PDF
Twitter APIs for #MediaHackday
PPTX
Lets have some fun with twilio open tok
KEY
Intro to developing for @twitterapi (updated)
PDF
Let's read code: the python-requests library
PPTX
XML External Entity (XXE)
PDF
Djangocon 2014 angular + django
PDF
DEFCON 23 - Miaubiz - put on your tinfo_t hat
PDF
Building a Production-ready Predictive App for Customer Service - Alex Ingerm...
PDF
Mining Georeferenced Data
KEY
Building @Anywhere (for TXJS)
PDF
Intro to developing for @twitterapi
How to get data from twitter (by hnnrrhm)
Streaming twitter data using kafka
PyLecture2 -NetworkX-
TwitterKitではじめる OAuthスピードクッキング
Social media analysis in R using twitter API
Tips and tricks for building api heavy ruby on rails applications
Api Doc
Api Doc
Development of Twitter Application #3 - OAuth
Twitter APIs for #MediaHackday
Lets have some fun with twilio open tok
Intro to developing for @twitterapi (updated)
Let's read code: the python-requests library
XML External Entity (XXE)
Djangocon 2014 angular + django
DEFCON 23 - Miaubiz - put on your tinfo_t hat
Building a Production-ready Predictive App for Customer Service - Alex Ingerm...
Mining Georeferenced Data
Building @Anywhere (for TXJS)
Intro to developing for @twitterapi
Ad

More from fathimafancyjeweller (20)

PDF
Which of the following statements about financial intermediaries is .pdf
PDF
Why must each network router interface belong to a different network.pdf
PDF
What are the similarities and differences between the seppuku of the.pdf
PDF
Which of the following are considered withdrawals from an economy.pdf
PDF
What measures have the EU (or member nations) taken to mitigate the .pdf
PDF
What types of powers are given to the states by the Constitution.pdf
PDF
Using the balanced equations given below, determine the overall mola.pdf
PDF
True or false Income statement profit and loss statement shows com.pdf
PDF
Thomas Rinks and Joseph Shields developed Psycho Chihuahua, a carica.pdf
PDF
The government uses part of payroll taxes to pay for Social Security..pdf
PDF
The financial statements are prepared from the unadjusted trial balan.pdf
PDF
Should the US conform to international standardsSolutionYes, .pdf
PDF
Research and describe a tool that can be used to test for web server.pdf
PDF
Question 11 of 24 In E. coli, three structural Deand enzymes A. D, an.pdf
PDF
Question 1. Dscribe the change you observed when you added 1 mL of 0.pdf
PDF
Prove that a group of order 255 is a cyclic.SolutionLet G be a .pdf
PDF
Choose one field such as academia, health, consulting, business, man.pdf
PDF
An intercalating agent 1. Inserts between base pairs of the two str.pdf
PDF
a trait is considered discrete when 1-The traits are intermediat.pdf
PDF
Please show your input and output screenshot of matlab. Show all wor.pdf
Which of the following statements about financial intermediaries is .pdf
Why must each network router interface belong to a different network.pdf
What are the similarities and differences between the seppuku of the.pdf
Which of the following are considered withdrawals from an economy.pdf
What measures have the EU (or member nations) taken to mitigate the .pdf
What types of powers are given to the states by the Constitution.pdf
Using the balanced equations given below, determine the overall mola.pdf
True or false Income statement profit and loss statement shows com.pdf
Thomas Rinks and Joseph Shields developed Psycho Chihuahua, a carica.pdf
The government uses part of payroll taxes to pay for Social Security..pdf
The financial statements are prepared from the unadjusted trial balan.pdf
Should the US conform to international standardsSolutionYes, .pdf
Research and describe a tool that can be used to test for web server.pdf
Question 11 of 24 In E. coli, three structural Deand enzymes A. D, an.pdf
Question 1. Dscribe the change you observed when you added 1 mL of 0.pdf
Prove that a group of order 255 is a cyclic.SolutionLet G be a .pdf
Choose one field such as academia, health, consulting, business, man.pdf
An intercalating agent 1. Inserts between base pairs of the two str.pdf
a trait is considered discrete when 1-The traits are intermediat.pdf
Please show your input and output screenshot of matlab. Show all wor.pdf
Ad

Recently uploaded (20)

PPTX
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
PPTX
Tissue processing ( HISTOPATHOLOGICAL TECHNIQUE
PDF
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
PDF
FourierSeries-QuestionsWithAnswers(Part-A).pdf
PDF
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
PDF
Module 4: Burden of Disease Tutorial Slides S2 2025
PDF
Chinmaya Tiranga quiz Grand Finale.pdf
PPTX
Introduction-to-Literarature-and-Literary-Studies-week-Prelim-coverage.pptx
PDF
O7-L3 Supply Chain Operations - ICLT Program
PDF
2.FourierTransform-ShortQuestionswithAnswers.pdf
PPTX
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
PPTX
human mycosis Human fungal infections are called human mycosis..pptx
PDF
Classroom Observation Tools for Teachers
PDF
Supply Chain Operations Speaking Notes -ICLT Program
PPTX
Cell Types and Its function , kingdom of life
PDF
GENETICS IN BIOLOGY IN SECONDARY LEVEL FORM 3
PPTX
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
PDF
VCE English Exam - Section C Student Revision Booklet
PPTX
Pharma ospi slides which help in ospi learning
PDF
RMMM.pdf make it easy to upload and study
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
Tissue processing ( HISTOPATHOLOGICAL TECHNIQUE
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
FourierSeries-QuestionsWithAnswers(Part-A).pdf
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
Module 4: Burden of Disease Tutorial Slides S2 2025
Chinmaya Tiranga quiz Grand Finale.pdf
Introduction-to-Literarature-and-Literary-Studies-week-Prelim-coverage.pptx
O7-L3 Supply Chain Operations - ICLT Program
2.FourierTransform-ShortQuestionswithAnswers.pdf
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
human mycosis Human fungal infections are called human mycosis..pptx
Classroom Observation Tools for Teachers
Supply Chain Operations Speaking Notes -ICLT Program
Cell Types and Its function , kingdom of life
GENETICS IN BIOLOGY IN SECONDARY LEVEL FORM 3
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
VCE English Exam - Section C Student Revision Booklet
Pharma ospi slides which help in ospi learning
RMMM.pdf make it easy to upload and study

Write a Python program that extracts 1000 unique links from Twitter .pdf

  • 1. Write a Python program that extracts 1000 unique links from Twitter using Tweepy. Desired output only has screen name and extended urls. Solution import tweepy /tweepy/tweepy import tweepy /tweepy/tweepyimport csv#Twitter API credentialsconsumer_key = ""consumer_secret = ""access_key = ""access_secret = ""def get_all_tweets(screen_name):#Twitter only allows access to a users most recent 3240 tweets with this method#authorize twitter, initialize tweepyauth = tweepy.OAuthHandler(consumer_key, consumer_secret)auth.set_access_token(access_key, access_secret)api = tweepy.API(auth)#initialize a list to hold all the tweepy Tweetsalltweets = []#make initial request for most recent tweets (200 is the maximum allowed count)new_tweets = api.user_timeline(screen_name = screen_name,count=200)#save most recent tweetsalltweets.extend(new_tweets)#save the id of the oldest tweet less oneoldest = alltweets[- 1].id - 1#keep grabbing tweets until there are no tweets left to grabwhile len(new_tweets) > 0:print "getting tweets before %s" % (oldest)#all subsiquent requests use the max_id param to prevent duplicatesnew_tweets = api.user_timeline(screen_name = screen_name,count=200,max_id=oldest)#save most recent tweetsalltweets.extend(new_tweets)#update the id of the oldest tweet less oneoldest = alltweets[-1].id - 1print "...%s tweets downloaded so far" % (len(alltweets))#transform the tweepy tweets into a 2D array that will populate the csvouttweets = [[tweet.id_str, tweet.created_at, tweet.text.encode("utf-8")] for tweet in alltweets]#write the csvwith open('%s_tweets.csv' % screen_name, 'wb') as f:writer = csv.writer(f) writer.writerow(["id","created_at","text"])writer.writerows(outtweets)passif __name__ == '__main__':#pass in the username of the account you want to downloadget_all_tweets("J_tsar")