# 申请 twitter 开发者帐号 & 应用 token
consumer_key = "b5n...W1c"
consumer_secret = "RGOGO...rbGvh"
access_token = "10177...9Gwc5"
access_token_secret = "0ugoL...WsgxW"
# 填写twitter提供的开发Key和secret + 提交你的Key和secret
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
api = tweepy.API(auth, wait_on_rate_limit=True, wait_on_rate_limit_notify=True)
# 获取指定用户信息 api.get_user(username)
# Get the User object for twitter...
user = api.get_user('twitter')
# 用户关注人
api.friends(username, count=10):
# 用户粉丝
api.followers(username, count=10)
# 关键字搜索
results = api.search(q=query, lang=language)
import tweepy
def twitter_user(username):
""" 获取 twitter 用户数据"""
user = api.get_user(username, timeout=100)
print('------------- User ---------')
user_info = {
'id': user.id_str,
'nickname': user.name,
'location': user.location,
'screen_name': user.screen_name,
'intro': user.description, # emojo表情 处理
'protected': user.protected,
'fans_num': user.followers_count,
'focus_num': user.friends_count,
'create_time': str(user.created_at),
'verified': user.verified,
'tweet_num': user.statuses_count,
'photo': user.profile_image_url.replace('normal', '400x400'), # profile_image_url_https
'url': user.url,
}
# 下载头像图片
try:
save_path = config.STATIC_PIC_PATH + '/twitter_user/' + user_info['id'] + '.png'
urllib.request.urlretrieve(user_info['photo'], os.path.join(save_path))
except Exception as e:
print('photo download Error: ', user_info, '\n', traceback.format_exc())
# print(json.dumps(user_info, indent=4, ensure_ascii=False))
return user_info
# 获取指定用户指定数量的发帖数据 tweets = api.user_timeline(username)
def twitter_tweet(username, count):
""" twitter tweet data"""
# tweets = api.user_timeline(username)
for tweet in tweepy.Cursor(api.user_timeline, id=screen_name, tweet_mode='extended').items(count):
tweet_id = tweet.id_str
pub_time = str(tweet.created_at)
tool = tweet.source
repost_num = tweet.retweet_count
like_num = tweet.favorite_count
content = tweet.full_text
lang = tweet.lang
# 图片 & 视频 处理 (1~4四张照片 或 一个视频/GIF)
media = tweet.entities.get('media')
# 引用/转推/回复 过滤
is_reply = tweet.in_reply_to_status_id_str # 回复 -- 若是回复,原始推文ID, 不是则为 None
is_quote = tweet.is_quote_status # 引用 -- 是否引用推文
is_retweet = tweet.retweeted_status
# 获取指定用户指定帖子id之后的发帖数据
for tweet in tweepy.Cursor(api.user_timeline, id=username, since_id=since_id, tweet_mode='extended').items():
pass
参考:
https://github.com/tweepy/tweepy
https://tweepy.readthedocs.io/en/latest/
https://developer.twitter.com/en/support/twitter-api