Lecture Note
University
University of California San DiegoCourse
DSC 207R | Python for Data SciencePages
1
Academic year
2023
anon
Views
16
Explore Twitter Search In this tutorial, we'll talk about how to use the Twitter API to look up information on a specific subject. At the end of this article, you should be able to use Python to obtain searchresults from Twitter, filter out duplicates using a for loop and list lookup, and look at a JSONdata structure. We'll utilize the Twitter API to look for tweets with particular subjects orhashtags in the section that follows. A tweet is made up of a 140-character string as well asadditional information about it, such as the dictionary's keys, which are components of thestatuses list in the notebook. With the use of this metadata, we will be able to gather usefulinformation about tweets that are returned in response to our search. Moving on to the notebook, we will search for some trends. For instance, we will look for tweets associated with the hashtag endangered species day which is a trending topic. Wecan also search for MTV awards, which was a topic discussed earlier. To search for tweetsusing Twitter API, we need to use a function that takes in the topic and the count of tweetswe want back as arguments. We will store the results in a variable called "number". Whenwe run the function, it returns all the recent records it finds up to a maximum of the count. Twitter often returns duplicate records on a subject. To clean the data, we will use a for loop to create a slice of data called "statuses" containing unique statuses. We will check ifthe text of each status is already in "all_text" and if it is not, we will append it to"filtered_statuses". At the end of the for loop, we will assign "filtered_statuses" to the"statuses" object. We can display the statuses by slicing out the text from the statuses objector by using the JSON dumps. The length of the filtered statuses object will be less than theoriginal statuses object as duplicates will have been removed. The content, screen names, and hashtags for each record will then be extracted from the statuses data structure and organized into lists. "status texts" will be the name of the first list.We'll use fields like retweet count and retweeted as the index to do this. In order to extractthe text, screen names, and hashtags, we will use any data that we had retweeted for theMTV awards.
Twitter API Tutorial: Searching for Tweets with Python
Please or to post comments