Post to Twitter using Python

Manipulate the Twitter API from Python Post a tweet to your account!

By the way, the Python Twitter Tools used this time is a package that can not only post, but also get Twitter search results and send direct messages.

Set environment

MacBook Air (13-inch, Mid 2011) Processor: 1.8 GHz Intel Core i7 Memory: 4 GB 1333 MHz DDR3 OS version: 10.11.5 Python: 3.5.2

Preparation

This time we will use Python Twitter Tools.

There seems to be a way to install it with pip, but I downloaded the file from here and installed it.

https://pypi.python.org/pypi/twitter

After downloading, move to the unzipped folder and install. (Since the version is written in the folder, please enter it according to the DL.)

$cd twitter-x.xx.x
$python setup.py install

When installing with pip

$pip install twitter

Register the application on Twitter

Register the Twitter application from the following page to use the API. Use the same account that you normally post.

https://apps.twitter.com/

After registering, select the "Keys and Access Tokens" tab and select Get API key, API secret, Access token, Access token secret.

image

Let's post!

Now, let's put various Tokens in the following code and execute it.

post.py


from twitter import Twitter, OAuth

access_token = "Fill in your own"
access_token_secret = "Fill in your own"
api_key = "Fill in your own"
api_secret = "Fill in your own"

t = Twitter(auth = OAuth(access_token, access_token_secret, api_key, api_secret))

text = 'I tweeted using Python.'
statusUpdate = t.statuses.update(status=text)

#Output of raw post data
print(statusUpdate)

#Output of post data with narrowed down elements
print(statusUpdate['user']['screen_name'])
print(statusUpdate['user']['name'])
print(statusUpdate['text'])

When you run it, it first prints a very long string, which is all the data contained in one post.

It's hard to tell what kind of content was tweeted, so it looks like this. In the last 3 lines, the screen name, user name, and post body are extracted and output.

post.py


#Output of post data with narrowed down elements
print(statusUpdate['user']['screen_name'])
print(statusUpdate['user']['name'])
print(statusUpdate['text'])

I think you should edit the inside of [] according to the data you want to extract. For example, you can get a profile introduction by typing ['description'].

It was a handy introduction to Python Twitter Tools that you can post and extract.

Recommended Posts

Post to Twitter using Python
[Lambda] [Python] Post to Twitter from Lambda!
Easily post to twitter with Python 3
Regularly post to Twitter using AWS lambda!
Search Twitter using Python
Post youtube soaring title to twitter with python3
Post from Python to Slack
Post to vim → Python → Slack
Post to slack with Python 3
Start to Selenium using python
Post to Slack in Python
Post to your account using the API on Twitter
How to install python using anaconda
Post from python to facebook timeline
Post multiple Twitter images with python
Post images from Python to Tumblr
[Nanonets] How to post Memo [Python]
Tweet Now Playing to Twitter using the Spotify API. [Python]
Updated to Python 2.7.9
From Python to using MeCab (and CaboCha)
Introduction to Discrete Event Simulation Using Python # 1
Tweet using the Twitter API in Python
Start using Python
Log in to Slack using requests in Python
Try to operate Excel using Python (Xlwings)
Dump BigQuery tables to GCS using Python
Python: Negative / Positive Analysis: Twitter Negative / Positive Analysis Using RNN-Part 1
Introduction to Discrete Event Simulation Using Python # 2
Get RSS feed using Python + pandas → Post to Mattermost & Save to DB
Scraping using Python
"Backport" to python 2
Post to slack with Python 3
Post to Twitter using Python
#Monte Carlo method to find pi using Python
Procedure to use TeamGant's WEB API (using python)
Image characters and post to slack (python slackbot)
Introducing 4 ways to monitor Python applications using Prometheus
I want to email from Gmail using Python.
[First post] Question ・ How to sweep out python
Steps to create a Twitter bot with python
Convert STL to Voxel mesh using Python VTK
Post an article with an image to WordPress with Python
Operate Redmine using Python Redmine
How to install Python
Fibonacci sequence using Python
Changes from Python 3.0 to Python 3.5
Changes from Python 2 to Python 3.0
Data analysis using Python 0
Rewrite Python2 code to Python3 (2to3)
python decorator to retry
Introduction to Python language
Data cleaning using Python
Introduction to OpenCV (python)-(2)
Using Python #external packages
WiringPi-SPI communication using Python
Note to daemonize python
Age calculation using python
Introducing Python 2.7 to CentOS 6.6
Connect python to mysql
[Python MinMaxScaler] Normalize to 0 ~ 1
Name identification using python
Notes using Python subprocesses