如何提取推文并将其存储在CSV文件中?

时间:2020-02-26 10:01:01

标签: python twitter export-to-csv

我已经编写了这段代码来保存我从特定用户提取的idtweet,但是问题是它只保存了索引为50的第一条推文,我尝试了计数器,但没有任何反应。

a=50
for info in tweets[:a]:
   with open(userID+'.csv', 'w', newline='') as file:
     writer = csv.writer(file)
     writer.writerow(["id", "tweet"])
     writer.writerow([info.id,info.full_text+ "\n"])
     a-=1

1 个答案:

答案 0 :(得分:1)

使用附加模式,而不要写入。

with open(userID+'.csv', 'a', newline='') as file:

'w':写模式实际上替换了现有内容。 'a':追加模式将数据添加到现有文件中。 在这里阅读: https://www.guru99.com/reading-and-writing-files-in-python.html