Save the search results on Twitter to CSV.

A script that saves search results on Twitter to CSV. As usual, if there is no CSV file to write to, create it, Because it is a specification, if you execute it from the second time onwards, the CSV header will be duplicated, so It is necessary to take measures such as renaming the file name.

twcsv.py



#!/user/bin/env python
# -*- coding: utf-8 -*-
from requests_oauthlib import OAuth1Session
import csv
import json
import sys, codecs



search_words = raw_input(u"Keyword?: ")

C_KEY = "******************************************"
C_SECRET = "******************************************"
A_KEY = "******************************************"
A_SECRET = "******************************************"



def Search_words():
	url = "https://api.twitter.com/1.1/search/tweets.json?"
	params = {
			"q": unicode(search_words, "utf-8"),
			"lang": "ja",
			"result_type": "recent",
			"count": "100"
			}
	tw = OAuth1Session(C_KEY,C_SECRET,A_KEY,A_SECRET)
	req = tw.get(url, params = params)
	tweets = json.loads(req.text)

	f = open("tweetsearch.csv" , "ab")
	writer = csv.writer(f)
	writer.writerow(["datetime", "id", "name", "text"])

	for tweet in tweets["statuses"]:
		time = (tweet["created_at"])
		id = (tweet["user"]["screen_name"].encode("utf-8"))
		name = (tweet["user"]["name"].encode("utf-8"))
		text = (tweet["text"].encode("utf-8"))
	
		writer.writerow([time, id, name, text])
	
	f.close()
	
	return Search_words
	
Search_words()

If you like the format of the data to be written to CSV, it may be easier to use by modifying it.

writer.writerow(["datetime", "id", "name", "text"])

Write the CSV header and the first line of CSV with. Because it is a header, you only have to write it once.

writer.writerow([time, id, name, text])

Write the content of the tweet acquired in this part to CSV.

What kind of person is tweeting what kind of tweet, when there are many tweets, etc. I think it's interesting to make a graph, but I don't have enough knowledge so far this time.

Recommended Posts

Save the search results on Twitter to CSV.
Search for Twitter keywords with tweepy and write the results to Excel
Convert XLSX to CSV on the command line
Try to estimate the number of likes on Twitter
Save tweets containing specific keywords in CSV on Twitter
Post to your account using the API on Twitter
Save images on the web to Drive with Python (Colab)
I don't tweet, but I want to use tweepy: just display the search results on the console
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
After calling the Shell file on Python, convert CSV to Parquet.
Post the subject of Gmail on twitter
Hit the Rakuten Ranking API to save the ranking of any category in CSV
Program to get favorite images on Twitter
You search commandlinefu on the command line
Preprocessing with Python. Convert Nico Nico Douga tag search results to CSV format
Automatically save images and videos hit by Twitter search to iPhone using Pythonista3
I stumbled on the character code when converting CSV to JSON in Python
[Python] How to save images on the Web at once with Beautiful Soup
I tried to save the data with discord
Twitter streaming client to enjoy in the terminal
Introduction to Python with Atom (on the way)
Save the object to a file with pickle
Recursively search the specified directory to see the file
twitter on python3
A story about a Python beginner trying to get Google search results using the API
[Latest] How to use Python library to save Google image search & use Chrome Driver on ubuntu
Think about how to program Python on the iPad
How to put Takoyaki Oishikunaru on the segment tree
[Python] This is easy! Search for tweets on Twitter
I tried to touch the CSV file with Python
Save the graph drawn by pyqtgraph to an image
Tweet the triple forecast of the boat race on Twitter
How to save a table scraped by python to csv
Apply Google Search Console to websites created on Heroku
Search and save images of Chino Kafu from Twitter
How to enjoy Python on Android !! Programming on the go !!
I tried to notify the honeypot report on LINE
A Python script that goes from Google search to saving the Search results page at once