[PYTHON] Search for Twitter keywords with tweepy and write the results to Excel

I made a process to search Twitter by keyword with tweepy and write the result to Excel, so make a note of it.

See below. http://statsbeginner.hatenablog.com/entry/2015/10/21/131717 http://kasoutuuka.org/twitter-tweepy http://qiita.com/Kamo3167/items/b13531938a68234f19bf http://kivantium.hateblo.jp/entry/2015/01/03/000225

Source

# -*- coding:utf-8 -*-

import tweepy
import datetime
import xlsxwriter

#Get key
CONSUMER_KEY = '****************'
CONSUMER_SECRET = '****************'
#Create an instance of the OAuthHandler class
auth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET)
ACCESS_TOKEN = '****************'
ACCESS_SECRET = '****************'
auth.set_access_token(ACCESS_TOKEN, ACCESS_SECRET)

#OAuth authentication
api = tweepy.API(auth)

wb = xlsxwriter.Workbook('tweet.xlsx')
ws = wb.add_worksheet("teet")

#Creating an excel header
format = wb.add_format()
format.set_num_format('yy/mm/dd')
ws.write(0, 0, "name")
ws.write(0, 1, "user name")
ws.write(0, 2, "tweet")
ws.write(0, 3, "create date")

i = 1
#Search in twitter and write the result to excel
for status in api.search(q='"Aikatsu"', lang='ja', result_type='recent',count=100):
    ws.write(i, 0, status.user.name)
    ws.write(i, 1, status.user.screen_name)
    ws.write(i, 2, status.text)
    ws.write(i, 3, status.created_at+ datetime.timedelta(hours=9),format)
    i = i + 1
wb.close()

result

Write your name, user name, tweet, and posting date in Excel like this. image

What to do before coding with tweepy

--Create a Twitter account (of course) -Register the application with Twitter Application Management

I register with Twitter Application Management, but a website is required, but I don't use such a thing, so if you enter an appropriate URL, it's OK.

image

One thing to keep in mind when registering is that you need a ** phone number ** for your Twitter account. When I registered the phone number, I entered the phone number and the 6-digit number sent to the SMS, but I couldn't register even though I had the phone number and the 6-digit verification number.

The cause is that the phone number has a leading 0. If 090-XXXX-YYYY, it must be 90XXXXYYYY. http://qiita.com/tk1024/items/644ead20793a6e869b83

If you can register the application, make a note of the following

Coding with tweepy

Coding is about OAuth authentication with the key information noted above, so only for searching.

ʻApi.search (q ='"Aikatsu "', lang ='ja', result_type ='recent', count = 100): ` This process searches for "Aikatsu" on Twitter as a keyword, and extracts only the regions of Japan. Get the latest 100 tweets.

Due to the specifications of tweepy, the maximum number of tweets that can be acquired is 100, so even if you do 101, you can only acquire 100.

See below for detailed settings. https://syncer.jp/twitter-api-matome/get/search/tweets

Miscellaneous feelings

Since the upper limit of the number of acquired tweets is 100, I have the impression that it is difficult to analyze using this. The implementation itself is easy, and in addition to getting tweets, you can also tweet itself, so I wonder if it could be used for automatic tweets or something like that.

Recommended Posts

Search for Twitter keywords with tweepy and write the results to Excel
Save the search results on Twitter to CSV.
Recursively get the Excel list in a specific folder with python and write it to Excel.
Repeat with While. Scripts to Tweet and search from the terminal
Touch around the twitter list with tweepy
Write the result of keyword search with ebaysdk to Google Spread Sheets
Causal reasoning and causal search with Python (for beginners)
Create a command to search for similar compounds from the target database with RDKit and check the processing time
For those who want to learn Excel VBA and get started with Python
Perform a Twitter search from Python and try to generate sentences with Markov chains.
It's too easy to access the Twitter API with rauth and I have her ...
[Note] How to write QR code and description in the same image with python
It is easy to execute SQL with Python and output the result in Excel
Crawling with Python and Twitter API 1-Simple search function
Next to Excel, for the time being, jupyter notebook
For those who want to write Python with vim
Install tweepy with pip and use it for API 1.1
I don't tweet, but I want to use tweepy: just display the search results on the console
Try hitting the Twitter API quickly and easily with Python
[pyqtgraph] Add region to the graph and link it with the graph region
[NetworkX] I want to search for nodes with specific attributes
Search for variables in pandas.DataFrame and get the corresponding row.
Streamline information gathering with the Twitter API and Slack bots
Create a Twitter BOT with the GoogleAppEngine SDK for Python
Introduction to Python for VBA users-Calling Python from Excel with xlwings-
"Deep copy" and "Shallow copy" to understand with the smallest example
Write to csv with Python
Search twitter tweets with python
Save the results of crawling with Scrapy to the Google Data Store
I tried cross-validation based on the grid search results with scikit-learn
Crawling with Python and Twitter API 2-Implementation of user search function
Switch the package to be installed for each environment with poetry
Try to write a program that abuses the program and sends 100 emails
Write a script to calculate the distance with Elasticsearch 5 system painless
The strongest way to use MeCab and CaboCha with Google Colab
To improve the reusability and maintainability of workflows created with Luigi
For the time being, I want to convert files with ffmpeg !!
Create and return a CP932 CSV file for Excel with Chalice