Tweepy - 未写入文本文件

问题描述 投票:0回答:1

我正在尝试使用Twitter中的历史Twitter API提取推文,然后将它们复制到文本文件中。但是,文本文件根本没有被写入。

我曾尝试写过一个CSV,虽然那也没用。它在Python 3.6上运行,并且安装了所有库。我没有收到任何表明文本文件有问题的错误消息。

import tweepy
import sys
import os
import codecs

CONSUMER_KEY = "" # These are removed for obvious reasons!
CONSUMER_SECRET = ""
ACCESS_TOKEN = ""
ACCESS_TOKEN_SECRET = ""

auth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET)
auth.set_access_token(ACCESS_TOKEN, ACCESS_TOKEN_SECRET)

api = tweepy.API(auth)

f = codecs.open('C:\\Users\\ctrh1\\Desktop\\tweets30apr.txt', "w", encoding="utf-8")

for tweet in tweepy.Cursor(api.search,
                       q="brexit",
                       count=100,
                       since="2019-04-28",
                       until="2019-04-29",
                       lang="en").items():
    print(tweet.text)
    f.write(tweet.text)

我希望将一些推文中的文本写入文件f,但是在我停止运行代码后它是空白的。

python tweepy codec
1个回答
0
投票

如果您有权使用此最小示例进行编写,也许您可​​以先尝试一下:

file_name = 'C:\\Users\\ctrh1\\Desktop\\tweets30apr.txt'

with open(file_name, 'w') as f:
    f.write("Hello World!")
© www.soinside.com 2019 - 2024. All rights reserved.