我尝试使用 Tweepy 将扩展推文保存到 CSV 文件,但推文只会继续显示,直到达到 140 个字符的限制。我将 truncated 参数设置为 False,并将 tweet_mode 设置为“扩展”,但我仍然无法在 CSV 文件中显示完整的推文。有什么想法吗?
#!/usr/bin/python
import tweepy
import csv #Import csv
auth = tweepy.auth.OAuthHandler('XXX', 'XXX')
auth.set_access_token('XXX', 'XXX')
api = tweepy.API(auth)
# Open/create a file to append data to
csvFile = open('file.csv', 'a')
#Use csv writer
csvWriter = csv.writer(csvFile)
for tweet in tweepy.Cursor(api.search,
q = "query",
truncated = False,
tweet_mode= "extended",
since = "2019-12-11",
until = "2019-12-18",
wait_on_rate_limit = True,
lang = "en").items():
if (not tweet.retweeted) and ('RT @' not in tweet.full_text):
csvWriter.writerow([tweet.created_at, tweet.full_text.encode('utf-8')])
print(tweet.created_at, tweet.full_text)
csvFile.close()
最佳答案
如 Passing parameters into the API method section of the Cursor Tutorial 中所述,传递给 tweepy.Cursor() 的关键字参数将在调用时传递给 api.search。
但是,api.search
方法不采用 tweet_mode
关键字参数 - 请参阅 API.search
在 tweepy 文档中。事实上,使用它的唯一 API 端点似乎是 api.lookup_users()
(参见source code)。
换句话说,tweet_mode="extended"
关键字参数似乎只是被 api.search
方法忽略了。如果你看source code它不会在最终的 API 调用中使用。
两个建议:
如果
tweet_mode
关键字参数未实际实现,您可能需要在 tweepy repo on Github 中提交错误报告。 .或者,尝试将代码分为两个步骤:第一步,使用
api.search
获取与您的搜索匹配的推文 ID 列表。然后,使用生成的推文 ID 列表并按照 this example 调用api.get_status
。如果他们的示例不起作用,那么这是从 tweepy 存储库中的错误报告开始的好地方!
关于python - 使用 Tweepy 将扩展推文保存到 CSV?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59401540/