12
11

More than 3 years have passed since last update.

Tweepyで出来る事まとめ

Posted at

概要

Tweepyで出来る事をまとめる。

HomeのTimeline Tweetsを取得


public_tweets = api.home_timeline()
for tweet in public_tweets:
    print(tweet.text)

あるユーザーのモデルを取得&モデルのメソッドを使用


user = api.get_user('twitter')
print(user.screen_name)
print(user.followers_count)
for friend in user.friends():
   print(friend.screen_name)

モデルのメソッドの内容


フォロワーを全てフォローする


for follower in tweepy.Cursor(api.followers).items():
    follower.follow()

ツイートの取得

指定した単語を含むツイートを取得


for tweet in tweepy.Cursor(api.search, q='指定した単語').items(10):
    print(tweet)

特定のユーザーのツイートを取得


[tweet.text for tweet in tweepy.Cursor(api.user_timeline, id="Qiita").items(10) if (list(tweet.text)[:2]!=['R', 'T']) & (list(tweet.text)[0]!='@')]

ページ毎に取得


for page in tweepy.Cursor(api.user_timeline).pages(3):
    # pageはstatusのリストです。
    print(len(page))

status objectに含まれるコンテンツ

  • created_at : The time the status was posted.
  • id : The ID of the status.
  • id_str : The ID of the status as a string.
  • text : The text of the status.
  • entities : The parsed entities of the status such as hashtags, URLs etc.
  • source : The source of the status.
  • source_url : The URL of the source of the status.
  • in_reply_to_status_id : The ID of the status being replied to.
  • in_reply_to_status_id_str : The ID of the status being replied to in as a string.
  • in_reply_to_user_id : The ID of the user being replied to.
  • in_reply_to_user_id_str : The ID of the user being replied to as a string.
  • in_reply_to_screen_name : The screen name of the user being replied to
  • user : The User object of the poster of the status.
  • geo : The geo object of the status.
  • coordinates : The coordinates of the status.
  • place : The place of the status.
  • contributors : The contributors of the status.
  • is_quote_status : Indicates whether the status is a quoted status or not.
  • retweet_count : The number of retweets of the status.
  • favorite_count : The number of likes of the status.
  • favorited : Indicates whether the status has been favourited by the authenticated user or not.
  • retweeted : Indicates whether the status has been retweeted by the authenticated user or not. possibly_sensitive : Indicates whether the status is sensitive or not.
  • lang : The language of the status.
# the ID of the status 
id = 1268080321590935553

# fetching the status 
status = api.get_status(id) 

# printing the information 
print("The status was created at : " + str(status.created_at)) 
print("The id is : " + str(status.id)) 
print("The id_str is : " + status.id_str) 
print("The text is : " + status.text) 
print("The entitities are : " + str(status.entities)) 
print("The source is : " + status.source) 
print("The source_url is : " + status.source_url) 


print("The in_reply_to_status_id is : " + str(status.in_reply_to_status_id)) 
print("The in_reply_to_status_id_str is : " + str(status.in_reply_to_status_id_str)) 
print("The in_reply_to_user_id is : " + str(status.in_reply_to_user_id)) 
print("The in_reply_to_user_id_str is : " + str(status.in_reply_to_user_id_str)) 
print("The in_reply_to_screen_name is : " + str(status.in_reply_to_screen_name)) 


print("The poster's screen name is : " + status.user.screen_name) 
print("The geo is : " + str(status.geo)) 
print("The coordinates are : " + str(status.coordinates)) 
print("The place is : " + str(status.place)) 
print("The contributors are : " + str(status.contributors)) 
print("The is_quote_status is : " + str(status.is_quote_status)) 
print("The retweet_count is : " + str(status.retweet_count)) 
print("The favorite_count is : " + str(status.favorite_count)) 

print("Has the authenticated user favourited the status? : " + str(status.favorited)) 
print("Has the authenticated user retweeted the status? " + str(status.retweeted)) 
print("Is the status possibly_sensitive? : " + str(status.possibly_sensitive)) 
print("The lang is : " + status.lang) 

# Result
# The status was created at : 2020-06-03 07:21:23
# The id is : 1268080321590935553
# The id_str is : 1268080321590935553
# The text is : Time really hits differently when you are
# ☝️holding a plank for a minute,
# ✌️washing your hands for 20 seconds, and… https://t.co/BBg2RIamGy
# The entitities are : {'hashtags': [], 'symbols': [], 'user_mentions': [], 'urls': [{'url': 'https://t.co/BBg2RIamGy', 'expanded_url': # 'https://twitter.com/i/web/status/1268080321590935553', 'display_url': 'twitter.com/i/web/status/1…', 'indices': [116, 139]}]}
# The source is : Twitter Web App
# The source_url is : https://mobile.twitter.com
# The in_reply_to_status_id is : None
# The in_reply_to_status_id_str is : None
# The in_reply_to_user_id is : None
# The in_reply_to_user_id_str is : None
# The in_reply_to_screen_name is : None
# The poster's screen name is : geeksforgeeks
# The geo is : None
# The coordinates are : None
# The place is : None
# The contributors are : None
# The is_quote_status is : False
# The retweet_count is : 2
# The favorite_count is : 30
# Has the authenticated user favourited the status? : False
# Has the authenticated user retweeted the status? False

tweetのidが判明した際にピンポイントで検索する方法

https://twitter.com/intent/retweet?tweet_id="調べたいtweetのid"

フォロー、フォロワー数、ユーザーのフォローを取得

api.me()

api.me().followers_count
# フォロワー数確認

api.me().friends_count
# フォロー数確認

api.me().description
# プロフィールコメント表示

api.friends()
# 自分がフォローをしているユーザーのUser型の値を取得


api.create_friendship(screen_name='QiitaJobs')
# @QiitaJobsをフォローする

フォローをする際に、

tweepy.error.TweepError: Read-only application cannot POST.

のようにエラーが出た場合、こちらの対処法を講じる

いいねしたツイートの表示


for item in api.favorites(screen_name='Taro_Kawasaki_J', count=50): 
    json_item=item._json
    print(json_item)

# この際、jsonの中に格納されている日本語は全てユニコードで格納されている。

likeの限界に関して

you do have a limit of a maximum of 180 GET requests every 15 minutes. Now, a GET request is to pull information from Twitter, which would be pulling the tweet you want to like; there’s nothing I can see that limits the number of likes you can send. Using your app you could then pull up a maximum of 17,280 tweets per day to like.

--- 参考
https://qiita.com/kitarikes/items/543ab75a379f0bf903cc
https://www.geeksforgeeks.org/python-status-object-in-tweepy/
https://qiita.com/kitarikes/items/74b58bd28325d7d43a57
https://follows.com/blog/2017/08/how-tweets-safe-day#:~:text=It's%20also%20divided%20into%2015,GET%20requests%20every%2015%20minutes.
Tweepyで相互フォローの確認&フォロバとリムーブを自動化
Tweepyで検索に引っかかったツイートを自動ファボ

12
11
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
12
11