我想在特定时间段内获取包括关键字在内的所有推文。我有下面使用 Twitter API 的代码,但它只返回包含当天发布的关键字的推文。代码是用 Python 编写的,我使用 Twython 库。 我怎样才能得到上一年的所有推特,包括关键字“水”?
import json
# Enter your keys/secrets as strings in the following fields
credentials = {}
credentials['CONSUMER_KEY'] = "*********"
credentials['CONSUMER_SECRET'] = "*********"
credentials['ACCESS_TOKEN'] = "*********"
credentials['ACCESS_SECRET'] = "*********"
# Save the credentials object to file
with open("twitter_credentials.json", "w") as file:
json.dump(credentials, file)
# Import the Twython class
from twython import Twython
# Load credentials from json file
with open("twitter_credentials.json", "r") as file:
creds = json.load(file)
# Instantiate an object
python_tweets = Twython(creds['CONSUMER_KEY'], creds['CONSUMER_SECRET'])
# Create our query
query = {'q': 'water',
# 'result_type': 'popular',
# 'count': 10,
'lang': 'en',
}
import pandas as pd
# Search tweets
dict_ = {'user': [], 'date': [], 'text': [], 'favorite_count': []}
for status in python_tweets.search(**query)['statuses']:
dict_['user'].append(status['user']['screen_name'])
dict_['date'].append(status['created_at'])
dict_['text'].append(status['text'])
dict_['favorite_count'].append(status['favorite_count'])
# Structure data in a pandas DataFrame for easier manipulation
df = pd.DataFrame(dict_)
df.sort_values(by='favorite_count', inplace=True, ascending=False)
df.head(5)