使用 max_id 收集推文未按预期工作
Collecting Tweets using max_id is not working as expected
我目前正在使用 Twitter Api
进行推文搜索。但是,获取推文 ID 对我不起作用。
这是我的代码:
searchQuery = '#BLM' # this is what we're searching for
searchQuery = searchQuery + "-filter:retweets"
Geocode="39.8, -95.583068847656, 2500km"
maxTweets = 1000000 # Some arbitrary large number
tweetsPerQry = 100 # this is the max the API permits
fName = 'tweetsBLM.json' # We'll store the tweets in a json file.
sinceId = None
#max_id = -1 # initial search
max_id=1278836959926980609 # the last id of previous search
tweetCount = 0
print("Downloading max {0} tweets".format(maxTweets))
with open(fName, 'w') as f:
while tweetCount < maxTweets:
try:
if (max_id <= 0):
if (not sinceId):
new_tweets = api.search(q=searchQuery,lang="en", geocode=Geocode,
count=tweetsPerQry)
else:
new_tweets = api.search(q=searchQuery,lang="en",geocode=Geocode,
count=tweetsPerQry,
since_id=sinceId )
else:
if (not sinceId):
new_tweets = api.search(q=searchQuery, lang="en", geocode=Geocode,
count=tweetsPerQry,
max_id=str(max_id - 1) )
else:
new_tweets = api.search(q=searchQuery, lang="en", geocode=Geocode,
count=tweetsPerQry,
max_id=str(max_id - 1),
since_id=sinceId)
if not new_tweets:
print("No more tweets found")
break
for tweet in new_tweets:
f.write(jsonpickle.encode(tweet._json, unpicklable=False) +
'\n')
tweetCount += len(new_tweets)
print("Downloaded {0} tweets".format(tweetCount))
max_id = new_tweets[-1].id
except tweepy.TweepError as e:
# Just exit if any error
print("some error : " + str(e))
print('exception raised, waiting 15 minutes')
print('(until:', dt.datetime.now() + dt.timedelta(minutes=15), ')')
time.sleep(15*60)
break
print ("Downloaded {0} tweets, Saved to {1}".format(tweetCount, fName))
这段代码工作得很好。我最初 运行 它并获得了大约 40 000 条推文。然后我用 previous/initial 搜索的 last tweet
的 id
回到过去。然而,我很失望地看到没有推文了。我不敢相信一秒钟。我一定是哪里出错了,因为 #BLM
在过去的 2/3 个月里非常活跃。
非常欢迎任何帮助。谢谢
我可能已经找到答案了。使用 Twitter API
,无法获取较旧的推文(7 天或更早)。使用 max_id
来解决这个问题也是不可能的。
唯一的办法就是直播等7天以上
最后,还有这个link寻找旧的推文
https://pypi.org/project/GetOldTweets3/ 它是 Jefferson Henrique 原作的延伸
我目前正在使用 Twitter Api
进行推文搜索。但是,获取推文 ID 对我不起作用。
这是我的代码:
searchQuery = '#BLM' # this is what we're searching for
searchQuery = searchQuery + "-filter:retweets"
Geocode="39.8, -95.583068847656, 2500km"
maxTweets = 1000000 # Some arbitrary large number
tweetsPerQry = 100 # this is the max the API permits
fName = 'tweetsBLM.json' # We'll store the tweets in a json file.
sinceId = None
#max_id = -1 # initial search
max_id=1278836959926980609 # the last id of previous search
tweetCount = 0
print("Downloading max {0} tweets".format(maxTweets))
with open(fName, 'w') as f:
while tweetCount < maxTweets:
try:
if (max_id <= 0):
if (not sinceId):
new_tweets = api.search(q=searchQuery,lang="en", geocode=Geocode,
count=tweetsPerQry)
else:
new_tweets = api.search(q=searchQuery,lang="en",geocode=Geocode,
count=tweetsPerQry,
since_id=sinceId )
else:
if (not sinceId):
new_tweets = api.search(q=searchQuery, lang="en", geocode=Geocode,
count=tweetsPerQry,
max_id=str(max_id - 1) )
else:
new_tweets = api.search(q=searchQuery, lang="en", geocode=Geocode,
count=tweetsPerQry,
max_id=str(max_id - 1),
since_id=sinceId)
if not new_tweets:
print("No more tweets found")
break
for tweet in new_tweets:
f.write(jsonpickle.encode(tweet._json, unpicklable=False) +
'\n')
tweetCount += len(new_tweets)
print("Downloaded {0} tweets".format(tweetCount))
max_id = new_tweets[-1].id
except tweepy.TweepError as e:
# Just exit if any error
print("some error : " + str(e))
print('exception raised, waiting 15 minutes')
print('(until:', dt.datetime.now() + dt.timedelta(minutes=15), ')')
time.sleep(15*60)
break
print ("Downloaded {0} tweets, Saved to {1}".format(tweetCount, fName))
这段代码工作得很好。我最初 运行 它并获得了大约 40 000 条推文。然后我用 previous/initial 搜索的 last tweet
的 id
回到过去。然而,我很失望地看到没有推文了。我不敢相信一秒钟。我一定是哪里出错了,因为 #BLM
在过去的 2/3 个月里非常活跃。
非常欢迎任何帮助。谢谢
我可能已经找到答案了。使用 Twitter API
,无法获取较旧的推文(7 天或更早)。使用 max_id
来解决这个问题也是不可能的。
唯一的办法就是直播等7天以上
最后,还有这个link寻找旧的推文
https://pypi.org/project/GetOldTweets3/ 它是 Jefferson Henrique 原作的延伸