我正在使用此代码通过Search API提取推文。到目前为止,我设法用jsonpickle仅提取了整个JSON。但是,我只想从JSON中提取特定信息,例如tweet.id
,tweet.full_text
或tweet.favorite_count
,并将其存储在CSV文件中。有人知道我该如何在不更改max_id
/ since_id
结构的情况下修改代码吗?
import sys
import jsonpickle
import os
import tweepy
consumer_key = "XXXXXXXXXXXXX"
consumer_secret = "XXXXXXXXXXX"
auth = tweepy.AppAuthHandler(consumer_key, consumer_secret)
api = tweepy.API(auth, wait_on_rate_limit=True,
wait_on_rate_limit_notify=True)
if (not api):
print ("Can't Authenticate")
sys.exit(-1)
searchQuery = 'XXXX'
maxTweets = XXX # number of tweets one wants to extract
tweetsPerQry = 100 # the max tweets the API permits per query
fName = 'tweets_keyword' # storing the tweets in a text file
sinceId = None
tweets = []
max_id = -1
tweetCount=0
print("Downloading max {0} tweets".format(maxTweets))
with open(fName+".csv", 'w') as file:
while tweetCount < maxTweets:
try:
if (max_id <= 0):
if (not sinceId):
new_tweets = api.search(q=searchQuery, count=tweetsPerQry, lang = 'en', tweet_mode = 'extended')
else:
new_tweets = api.search(q=searchQuery, count=tweetsPerQry, lang = 'en',tweet_mode = 'extended',
since_id=sinceId)
else:
if (not sinceId):
new_tweets = api.search(q=searchQuery, count=tweetsPerQry,lang = 'en', tweet_mode = 'extended',
max_id=str(max_id - 1))
else:
new_tweets = api.search(q=searchQuery, count=tweetsPerQry,lang = 'en',tweet_mode = 'extended',
max_id=str(max_id - 1),
since_id=sinceId)
if not new_tweets:
print("No more tweets found")
break
for tweet in new_tweets:
file.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))
break
print ("Downloaded {0} tweets, Saved to {1}".format(tweetCount, fName))
Status
/ Tweet object本身具有所讨论信息的属性。您可以简单地使用这些而不是原始的JSON。
原始JSON按照标准表示为字典,因此您也可以像其他任何字典一样通过键来访问它。
此外,API
永远不会是假的。