【问题标题】:Save extended tweet to CSV with Tweepy?使用 Tweepy 将扩展推文保存到 CSV?
【发布时间】:2020-04-11 13:37:42
【问题描述】:

我正在尝试使用 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()

【问题讨论】:

    标签: python csv twitter tweepy


    【解决方案1】:

    Passing parameters into the API method section of the Cursor Tutorial 中所述,提供给tweepy.Cursor() 的关键字参数将在被调用时传递给api.search

    但是,api.search 方法不采用 tweet_mode 关键字参数 - 请参阅 tweepy 文档中的 API.search。事实上,唯一使用它的 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 存储库中的错误报告开始!

    【讨论】:

    • 谢谢!不幸的是,我现在得到这个属性错误:'Status' 对象没有属性'full_text'
    • 这是来自使用第一个 extended tweets 示例的 get_status() 调用的结果吗?
    猜你喜欢
    • 2015-06-17
    • 2017-09-04
    • 2021-04-27
    • 2019-09-10
    • 2019-02-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-05
    相关资源
    最近更新 更多