#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Downloads all tweets from a given user.
Uses twitter.Api.GetUserTimeline to retreive the last 3,200 tweets from a user.
Twitter doesn't allow retrieving more tweets than this through the API, so we get
as many as possible.
"""
from __future__ import print_function
import json
import sys
import twitter
CONSUMER_KEY = 'your-consumer-key'
CONSUMER_SECRET = 'your-consumer-secret'
ACCESS_TOKEN_KEY = 'your-token'
ACCESS_TOKEN_SECRET = 'your-token-secret'
def get_tweets(api=None, screen_name=None):
timeline = api.GetUserTimeline(screen_name=screen_name, count=200)
earliest_tweet = min(timeline, key=lambda x: x.id).id
print("getting tweets before:", earliest_tweet)
while True:
tweets = api.GetUserTimeline(
screen_name=screen_name, max_id=earliest_tweet, count=200
)
new_earliest = min(tweets, key=lambda x: x.id).id
if not tweets or new_earliest == earliest_tweet:
break
else:
earliest_tweet = new_earliest
print("getting tweets before:", earliest_tweet)
timeline += tweets
return timeline
if __name__ == "__main__":
api = twitter.Api(
CONSUMER_KEY, CONSUMER_SECRET, ACCESS_TOKEN_KEY, ACCESS_TOKEN_SECRET
)
# screen_name = 'ProfFaustus' # This OLD account has been disabled by Twitter/X.
screen_name = 'Dr_CSWright' # This is the NEW account of CSW on Twitter/X.
print(screen_name)
timeline = get_tweets(api=api, screen_name=screen_name)
with open('tweets/{}_timeline.json'.format(screen_name), 'w+') as f:
for tweet in timeline:
f.write(json.dumps(tweet._json))
f.write('\n')
忘了提醒,需要用户自行提供上述代码中的Key和Secret:
看的懂点,刚学python呢,哈哈哈,收藏,改天试试
加油加油:)
CSW 的 Twitter 账号已经被封禁了,所以上述脚本无法抓取了。
但是如果想抓取其他正常账号的推文,可以修改一下:
screen_name = 'ProfFaustus'
部分为不同账号即可。
以上代码是基于 twitter v1 的 api.
有些进一步的功能可能需要v2版本api,感兴趣的话,可以自行查询官方文档后尝试编写。
也欢迎您编写分享。
CSW 已经启用新的 Twitter / X 账号了,
可以修改一楼代码中的 screen_name 如下:
screen_name = 'Dr_CSWright'
Thanks for the update.
The original code has been updated:)