When I use the Premium version and get a tweet, the number of retweet_count, favorite_count, and quote_count related to RT comes back as zero.
Also, when using the Standard version, the number of favorite_count will come back as zero.
I’m using Tweepy to get both.
Please let me know if there is a way to improve this.
The source code is as follows. (Standard and Premium versions)
①(Premium)
import tweepy
import pandas as pd
from distutils.util import strtobool
import time
Consumer_key = 'XXXXXXXXXXX'
Consumer_secret = 'XXXXXXXXXXXXXXX'
Access_token = 'XXXXXXXXXXXXXXXXXXXXXX'
Access_secret = 'XXXXXXXXXXXXXXXXXXXXXXXXXXXX'
# OAuth認証
auth = tweepy.OAuthHandler(Consumer_key, Consumer_secret)
auth.set_access_token(Access_token, Access_secret)
api = tweepy.API(auth, wait_on_rate_limit=True, wait_on_rate_limit_notify=True)
#ツイート取得
data = []
followerDatas=[]
df=pd.read_csv('@studyaiinfo_follow.csv',encoding='cp932')
for a in df['Screen_name']:
print(a)
keyword="from:"+str(a)+" lang:ja"
results = api.search_full_archive(environment_name = 'dev',query = keyword,fromDate = 202112070000,toDate = 202112080000,maxResults=500)
data = api.get_user(a)
for r in results:
followerData = {}
followerData["tweet"] = r.text
followerData["created_at"] = r.created_at
followerData["is_RT"] = r.retweeted
for entitles in r.entities['urls']:
i=["['",entitles['expanded_url'],"']"]
followerData["entities.urls"] = ''.join(i)
followerData["id_str"] = r.id_str
followerData["retweet_count"] = r.retweet_count
followerData["favorite_count"] = r.favorite_count
followerData["source"] = r.source
followerData["lang"] = r.lang
followerData["in_reply_to_status_id_str"] = r.in_reply_to_status_id_str
followerData["in_reply_to_screen_name"] = r.in_reply_to_screen_name
followerData["quote_count"] = r.quote_count
followerData["user.id_str"] = data.id_str
followerData["user.name"] = data.name
followerData["user.screen_name"] = data.screen_name
followerData["user.friends_count"] = data.friends_count
followerData["user.followers_count"] = data.followers_count
followerDatas.append(followerData)
import pandas as pd
pd.set_option("display.max_rows", 1000)
df = pd.DataFrame(followerDatas).loc[:,["tweet","created_at","is_RT","entities.urls","id_str","retweet_count","favorite_count","source","lang","in_reply_to_status_id_str","in_reply_to_screen_name","quoted_status_id_str","user.id_str","user.name","user.screen_name","user.friends_count","user.followers_count"]]
#data.append(r.retweet_count)
#ファイル出力
fileName = input("ファイル名を入力してください:")
#df.to_csv(fileName + ".tsv", sep='\t', index=False)
#print("「" + fileName + ".tsv」が作成されました。")
df.to_csv(fileName + ".csv")
print("「" + fileName + ".csv」が作成されました。")
②question(standard)
import tweepy
import pandas as pd
from distutils.util import strtobool
import time
Consumer_key = 'XXXXXXXXXXX'
Consumer_secret = 'XXXXXXXXXXXXXXX'
Access_token = 'XXXXXXXXXXXXXXXXXXXXXX'
Access_secret = 'XXXXXXXXXXXXXXXXXXXXXXXXXXXX'
# OAuth認証
auth = tweepy.OAuthHandler(Consumer_key, Consumer_secret)
auth.set_access_token(Access_token, Access_secret)
api = tweepy.API(auth, wait_on_rate_limit=True, wait_on_rate_limit_notify=True)
#ツイート取得
data = []
#最大2200件のツイートを取得するためのページ
pages = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50]
followerDatas=[]
df=pd.read_csv('@kounosuke_data_follow.csv',encoding='cp932')
for page in pages:
for a in df['Screen_name']:
print(a)
try:
results = api.user_timeline(screen_name=a, count=200, page=page,include_rts=False)
data = api.get_user(a)
for r in results:
followerData = {}
followerData["tweet"] = r.text
followerData["created_at"] = r.created_at
followerData["is_RT"] = r.retweeted
for entitles in r.entities['urls']:
i=["['",entitles['expanded_url'],"']"]
followerData["entities.urls"] = ''.join(i)
followerData["id_str"] = r.id_str
followerData["retweet_count"] = r.retweet_count
followerData["favorite_count"] = r.favorite_count
followerData["source"] = r.source
followerData["lang"] = r.lang
followerData["in_reply_to_status_id_str"] = r.in_reply_to_status_id_str
followerData["in_reply_to_screen_name"] = r.in_reply_to_screen_name
followerData["user.id_str"] = data.id_str
followerData["user.name"] = data.name
followerData["user.screen_name"] = data.screen_name
followerData["user.friends_count"] = data.friends_count
followerData["user.followers_count"] = data.followers_count
followerDatas.append(followerData)
except tweepy.TweepError:
print('tweepy.TweepError')
continue
import pandas as pd
pd.set_option("display.max_rows", 1000)
df = pd.DataFrame(followerDatas).loc[:,["tweet","created_at","is_RT","entities.urls","id_str","retweet_count","favorite_count","source","lang","in_reply_to_status_id_str","in_reply_to_screen_name","quoted_status_id_str","user.id_str","user.name","user.screen_name","user.friends_count","user.followers_count"]]
#ファイル出力
fileName = input("ファイル名を入力してください:")
df.to_csv(fileName + ".csv")
print("「" + fileName + ".csv」が作成されました。")