I had something I wanted to do using the Twitter API, so I will leave a self-learning memo about how to use the API used this time.
Write down each version of the environment and tools used this time. python: 3.8.2 (pyenv: 1.2.18) pip: 20.0.2 tweepy: 3.8.0 (Maybe the dependencies: PySocks-1.7.1 certifi-2020.4.5.1 chardet-3.0.4 idna-2.9 oauthlib-3.1.0 requests-2.23.0 requests-oauthlib-1.3.0 six-1.14.0 tweepy-3.8.0 urllib3-1.25.9)
It's a memo of the record I followed, so I don't know. (I started with what is pip this time)
I referred to it in the Python environment settings. https://prog-8.com/docs/python-env There is even such support. I thought Progate was a great service (small average feeling). Thank you very much.
I also understand pip well. Since it was a Mac, it moved on its own. https://gammasoft.jp/python/python-library-install/
I did most of it with reference to the following article. https://qiita.com/kngsym2018/items/2524d21455aac111cdee https://www.torikun.com/entry/twitter-developer-api/
(Sieve, API Japanese) http://westplain.sakuraweb.com/translate/twitter/Documentation/REST-APIs/Public-API/The-Search-API.cgi
I decided to use a library called tweepy. http://docs.tweepy.org/en/latest/install.html
The reason is that I was caught in the search, and there may be a library that is easier to use related to other twitter APIs. There seems to be OAuth 1a authentication, but I will not use it this time
First, proceed as described in Getting Start.
import tweepy
consumer_key = 'consumer_key'
consumer_secret = 'consumer_secret'
access_token = 'access_token'
access_token_secret = 'access_token_secret'
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
api = tweepy.API(auth)
public_tweets = api.home_timeline()
for tweet in public_tweets:
print('-------------------------')
print(tweet.text)
api.update_status("TEST post from tweepy [get timeline complete] ")
In this operation check. Get TimeLine for the time being, and check the operation until posting when the processing is completed.
Next, let's execute an example of getting user information of a specific id. The following processing has been added.
# Get the User object for twitter...
user = api.get_user('zooshima_k')
print(user.screen_name)
print(user.followers_count)
for friend in user.friends():
print(friend.screen_name)
** Result ↓ **
zooshima_k
188
・ ・ ・ ・ (List of friend names)
I tried it with my account, but it came out brilliantly. You will see the number of followers and the names of the users you are following. It's interesting.
After this, I expected that Steraming would bring information. Unfortunately, it hasn't been possible since 2018. sad···
I had something I wanted to do, but I gave up. This is the end of the article. I may update it again when I can do something.
Recommended Posts