首页 > 解决方案 > 指定推文 Tweepy 返回的数量

问题描述

我正在从公共推文构建一个词云。我已经通过 Tweepy 连接到 API 并成功地让它返回与我的搜索词相关的推文,但由于某种原因只能让它返回 15 条推文。

import pandas as pd

# subject of word cloud
search_term = 'ENTER SEARCH TERM HERE'

# creating dataframe containing the username and corresponding tweet content relating to our search term
df = pd.DataFrame(
    [tweet.user.id, tweet.user.name, tweet.text] for tweet in api.search(q=search_term, lang="en")
)

# renaming columns of data frame
df.rename(columns={0 : 'user id'}, inplace=True)
df.rename(columns={1 : 'screen name'}, inplace=True)
df.rename(columns={2 : 'text'}, inplace=True)
df 

标签: pythonpandastwittertweepyword-cloud

解决方案


默认情况下,使用的标准搜索 APIAPI.search每页最多返回 15 条推文。
如果您想在每个请求中检索更多,则需要指定count参数,最多为 100。

如果您想要超过 100 个或保证金额,则需要考虑使用tweepy.Cursor.


推荐阅读