+ All Categories
Home > Documents > Sta Lecture: Python + Twitter filePlan for Today Use Python to gr ab and analyze data on a specic...

Sta Lecture: Python + Twitter filePlan for Today Use Python to gr ab and analyze data on a specic...

Date post: 29-Aug-2019
Category:
Upload: doanlien
View: 217 times
Download: 0 times
Share this document with a friend
36
Sta Lecture: Python + Twitter Pat Pannuto / Slides by Stephanie Triesenberg 1 / 36
Transcript
Page 1: Sta Lecture: Python + Twitter filePlan for Today Use Python to gr ab and analyze data on a specic hashtag from Twitter tweepy $ pip3 install tweepy

Sta� Lecture: Python + Twitter

Pat Pannuto / Slides by Stephanie Triesenberg 1 / 36

Page 2: Sta Lecture: Python + Twitter filePlan for Today Use Python to gr ab and analyze data on a specic hashtag from Twitter tweepy $ pip3 install tweepy

AnnouncementsFill out course evaluations!

we care a lot about what you think!

due Wednesday, 4/18

Todaystaff lectures

2 / 36

Page 3: Sta Lecture: Python + Twitter filePlan for Today Use Python to gr ab and analyze data on a specic hashtag from Twitter tweepy $ pip3 install tweepy

Plan for Today

3 / 36

Page 4: Sta Lecture: Python + Twitter filePlan for Today Use Python to gr ab and analyze data on a specic hashtag from Twitter tweepy $ pip3 install tweepy

Plan for TodayUse Python to grab and analyze data on aspeci�c hashtag from Twitter

4 / 36

Page 5: Sta Lecture: Python + Twitter filePlan for Today Use Python to gr ab and analyze data on a specic hashtag from Twitter tweepy $ pip3 install tweepy

Plan for TodayUse Python to grab and analyze data on aspeci�c hashtag from Twittertweepy$ pip3 install tweepy

5 / 36

Page 6: Sta Lecture: Python + Twitter filePlan for Today Use Python to gr ab and analyze data on a specic hashtag from Twitter tweepy $ pip3 install tweepy

Plan for TodayUse Python to grab and analyze data on aspeci�c hashtag from Twittertweepy$ pip3 install tweepy

created specifically to pull data from Twitter

6 / 36

Page 7: Sta Lecture: Python + Twitter filePlan for Today Use Python to gr ab and analyze data on a specic hashtag from Twitter tweepy $ pip3 install tweepy

Plan for TodayUse Python to grab and analyze data on aspeci�c hashtag from Twittertweepy$ pip3 install tweepy

created specifically to pull data from Twitter

no need to index into large dictionaries or format request URLs

7 / 36

Page 8: Sta Lecture: Python + Twitter filePlan for Today Use Python to gr ab and analyze data on a specic hashtag from Twitter tweepy $ pip3 install tweepy

Plan for TodayUse Python to grab and analyze data on aspeci�c hashtag from Twittertweepy$ pip3 install tweepy

created specifically to pull data from Twitter

no need to index into large dictionaries or format request URLs

matplotlibpip3 install matplotlib

8 / 36

Page 9: Sta Lecture: Python + Twitter filePlan for Today Use Python to gr ab and analyze data on a specic hashtag from Twitter tweepy $ pip3 install tweepy

Plan for TodayUse Python to grab and analyze data on aspeci�c hashtag from Twittertweepy$ pip3 install tweepy

created specifically to pull data from Twitter

no need to index into large dictionaries or format request URLs

matplotlibpip3 install matplotlib

good for plotting data

9 / 36

Page 10: Sta Lecture: Python + Twitter filePlan for Today Use Python to gr ab and analyze data on a specic hashtag from Twitter tweepy $ pip3 install tweepy

Plan for TodayUse Python to grab and analyze data on aspeci�c hashtag from Twittertweepy$ pip3 install tweepy

created specifically to pull data from Twitter

no need to index into large dictionaries or format request URLs

matplotlibpip3 install matplotlib

good for plotting data

"not the best out there, but easy to use" - Amrit

10 / 36

Page 11: Sta Lecture: Python + Twitter filePlan for Today Use Python to gr ab and analyze data on a specic hashtag from Twitter tweepy $ pip3 install tweepy

Let's get started!

11 / 36

Page 12: Sta Lecture: Python + Twitter filePlan for Today Use Python to gr ab and analyze data on a specic hashtag from Twitter tweepy $ pip3 install tweepy

Step 1: Starter Code

12 / 36

Page 13: Sta Lecture: Python + Twitter filePlan for Today Use Python to gr ab and analyze data on a specic hashtag from Twitter tweepy $ pip3 install tweepy

Step 1: Starter CodeHave the slides? Great! Starter code is here.

Don't have the slides? No problem! Go tohttps://tinyurl.com/398-pyt

13 / 36

Page 14: Sta Lecture: Python + Twitter filePlan for Today Use Python to gr ab and analyze data on a specic hashtag from Twitter tweepy $ pip3 install tweepy

Step 1: Starter CodeHave the slides? Great! Starter code is here.

Don't have the slides? No problem! Go tohttps://tinyurl.com/398-pytbot.py contains starter code for the Python program

14 / 36

Page 15: Sta Lecture: Python + Twitter filePlan for Today Use Python to gr ab and analyze data on a specic hashtag from Twitter tweepy $ pip3 install tweepy

Step 1: Starter CodeHave the slides? Great! Starter code is here.

Don't have the slides? No problem! Go tohttps://tinyurl.com/398-pytbot.py contains starter code for the Python program

secret.py will hold your API keys and tokensyou want to keep your API keys secret, which is why they are placed in aseparate file from your program.

if you choose to put your project on GitHub, DO NOT commit secret.py

15 / 36

Page 16: Sta Lecture: Python + Twitter filePlan for Today Use Python to gr ab and analyze data on a specic hashtag from Twitter tweepy $ pip3 install tweepy

Step 2: Generate Twitter API Keys andTokens

16 / 36

Page 17: Sta Lecture: Python + Twitter filePlan for Today Use Python to gr ab and analyze data on a specic hashtag from Twitter tweepy $ pip3 install tweepy

Step 2: Generate Twitter API Keys andTokensGo to https://apps.twitter.com

17 / 36

Page 18: Sta Lecture: Python + Twitter filePlan for Today Use Python to gr ab and analyze data on a specic hashtag from Twitter tweepy $ pip3 install tweepy

Step 2: Generate Twitter API Keys andTokensGo to https://apps.twitter.comNOTE: You will need a Twitter account from this point on.

18 / 36

Page 19: Sta Lecture: Python + Twitter filePlan for Today Use Python to gr ab and analyze data on a specic hashtag from Twitter tweepy $ pip3 install tweepy

Step 2: Generate Twitter API Keys andTokensGo to https://apps.twitter.comNOTE: You will need a Twitter account from this point on.

sign into Twitter, then click "Create New App" in the upper right

19 / 36

Page 20: Sta Lecture: Python + Twitter filePlan for Today Use Python to gr ab and analyze data on a specic hashtag from Twitter tweepy $ pip3 install tweepy

Step 2: Generate Twitter API Keys andTokensGo to https://apps.twitter.comNOTE: You will need a Twitter account from this point on.

sign into Twitter, then click "Create New App" in the upper right

fill out the name, description, and website for your applicaiton. Check the"Developer Agreement" box and finish creating your app!

20 / 36

Page 21: Sta Lecture: Python + Twitter filePlan for Today Use Python to gr ab and analyze data on a specic hashtag from Twitter tweepy $ pip3 install tweepy

Step 2: Generate Twitter API Keys andTokensGo to https://apps.twitter.comNOTE: You will need a Twitter account from this point on.

sign into Twitter, then click "Create New App" in the upper right

fill out the name, description, and website for your applicaiton. Check the"Developer Agreement" box and finish creating your app!

under the name of your application, find and click the "Keys and AccessTokens" tab

21 / 36

Page 22: Sta Lecture: Python + Twitter filePlan for Today Use Python to gr ab and analyze data on a specic hashtag from Twitter tweepy $ pip3 install tweepy

Step 2: Generate Twitter API Keys andTokens

22 / 36

Page 23: Sta Lecture: Python + Twitter filePlan for Today Use Python to gr ab and analyze data on a specic hashtag from Twitter tweepy $ pip3 install tweepy

Step 2: Generate Twitter API Keys andTokensConsumer Keys

located under Applicaiton Settings

copy and paste these keys into secret.py

23 / 36

Page 24: Sta Lecture: Python + Twitter filePlan for Today Use Python to gr ab and analyze data on a specic hashtag from Twitter tweepy $ pip3 install tweepy

Step 2: Generate Twitter API Keys andTokensConsumer Keys

located under Applicaiton Settings

copy and paste these keys into secret.py

Access Tokensscroll down and click "Authorize my Application"

located under "Your Access Token"

copy and paste these keys into secret.py

24 / 36

Page 25: Sta Lecture: Python + Twitter filePlan for Today Use Python to gr ab and analyze data on a specic hashtag from Twitter tweepy $ pip3 install tweepy

Why?

25 / 36

Page 26: Sta Lecture: Python + Twitter filePlan for Today Use Python to gr ab and analyze data on a specic hashtag from Twitter tweepy $ pip3 install tweepy

Why?Veri�cation!

26 / 36

Page 27: Sta Lecture: Python + Twitter filePlan for Today Use Python to gr ab and analyze data on a specic hashtag from Twitter tweepy $ pip3 install tweepy

Step 3: Let's get coding!

27 / 36

Page 28: Sta Lecture: Python + Twitter filePlan for Today Use Python to gr ab and analyze data on a specic hashtag from Twitter tweepy $ pip3 install tweepy

Step 3: Let's get coding!TODO: import libraries

import tweepy

import matplotlib

from secret import *

28 / 36

Page 29: Sta Lecture: Python + Twitter filePlan for Today Use Python to gr ab and analyze data on a specic hashtag from Twitter tweepy $ pip3 install tweepy

Step 4: Twitter

29 / 36

Page 30: Sta Lecture: Python + Twitter filePlan for Today Use Python to gr ab and analyze data on a specic hashtag from Twitter tweepy $ pip3 install tweepy

Step 4: TwitterTODO: create a new Twitter session

check out the tweepy documentation on getting started

use keys/tokens from secret.py to create the session

30 / 36

Page 31: Sta Lecture: Python + Twitter filePlan for Today Use Python to gr ab and analyze data on a specic hashtag from Twitter tweepy $ pip3 install tweepy

Step 4: TwitterTODO: create a new Twitter session

check out the tweepy documentation on getting started

use keys/tokens from secret.py to create the session

TODO: user inputask the user (you) what hashtag to search

31 / 36

Page 32: Sta Lecture: Python + Twitter filePlan for Today Use Python to gr ab and analyze data on a specic hashtag from Twitter tweepy $ pip3 install tweepy

Step 4: Twitter

32 / 36

Page 33: Sta Lecture: Python + Twitter filePlan for Today Use Python to gr ab and analyze data on a specic hashtag from Twitter tweepy $ pip3 install tweepy

Step 4: TwitterTODO: search for hashtag

create new Tweet objects for each result

we only need the time each tweet was posted, but other data may beuseful for debugging or other data analysis

33 / 36

Page 34: Sta Lecture: Python + Twitter filePlan for Today Use Python to gr ab and analyze data on a specic hashtag from Twitter tweepy $ pip3 install tweepy

Step 5: Let's graph it!

34 / 36

Page 35: Sta Lecture: Python + Twitter filePlan for Today Use Python to gr ab and analyze data on a specic hashtag from Twitter tweepy $ pip3 install tweepy

Step 5: Let's graph it!TODO: graph timestamps from tweets in a histogram

check out the matplotlib documentation on histograms and subplots

this is just one analysis you could do on Twitter data

try something on your own!

35 / 36

Page 36: Sta Lecture: Python + Twitter filePlan for Today Use Python to gr ab and analyze data on a specic hashtag from Twitter tweepy $ pip3 install tweepy

Congratulations!You now know how to grab data from Twitter and

display it in a meaningful way.

36 / 36


Recommended