python - 使用 Tweepy 将扩展推文保存到 CSV?

标签 python csv twitter tweepy

我尝试使用 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 调用中使用。

两个建议:

  1. 如果 tweet_mode 关键字参数未实际实现,您可能需要在 tweepy repo on Github 中提交错误报告。 .

  2. 或者,尝试将代码分为两个步骤:第一步,使用 api.search 获取与您的搜索匹配的推文 ID 列表。然后,使用生成的推文 ID 列表并按照 this example 调用 api.get_status 。如果他们的示例不起作用,那么这是从 tweepy 存储库中的错误报告开始的好地方!

关于python - 使用 Tweepy 将扩展推文保存到 CSV?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59401540/

相关文章:

Python 子进程用完文件描述符

python - 如何通过名称区分python中的方法和属性

Python的unittest,通过输入通过测试

python - 不懂Python的csv.reader对象

iphone - 如何将twitter、facebook集成到iPhone应用程序中?

python - pip UnicodeDecodeError : 'utf8' codec can't decode byte

git - 将加密的 csv 导入 Python 3

csv - 使用 NaN 将 CSV 数据加载到 AWS Redshift

java - Twitter4j 中的多个用户流?

twitter - 如何在 Pig 中使用 rcfilepigstorage