python - 我应该如何清理从 twitter API 检索到的这些数据?
问题描述
这个脚本我从 Twitter API 中检索推文并将它们保存到一个 csv 文件中。目前,问题是推文完全打印在 csv 文件中,并且在一些推文中间仍然包含空格的文本位以及诸如“\xe2\x80\x99”之类的文本应该有成为标点符号。我尝试了以下方法:
for tweet in tweepy.Cursor(api.search,q=search_words,lang="en",since=date_since, tweet_mode = "extended").items(200):
tweet = tweet.strip()
但出现以下错误:
AttributeError: 'Status' object has no attribute 'strip'
也尝试添加该行:
如果 tweet.find('\xe') :继续
得到了这个:
SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 0-2: truncated \xXX escape
这是我的完整代码:
import tweepy
import csv
consumer_key=""
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)
search_words = "gender -filter:retweets"
date_since = "2021-01-28"
#tweets = tweepy.Cursor(api.search, q=search_words, lang="en",since=date_since).items(100)
# open and create a file to append the data to
csvFile = open('results.csv', 'a')
csvWriter = csv.writer(csvFile)
# use the csv file
# loop through the tweets variable and add them to the CSV file
for tweet in tweepy.Cursor(api.search,q=search_words,lang="en",since=date_since, tweet_mode = "extended").items(200):
tweet.strip()
if tweet.find('\xe') : continue
csvWriter.writerow([tweet.full_text.encode('utf-8')])
print(tweet.created_at, tweet.full_text)
csvFile.close()
尝试运行此代码并查看 csv 文件以查看我自己遇到的问题。请让我知道我应该如何清理这些推文。
解决方案
推荐阅读
- javascript - 在新的谷歌网站中重定向表单
- java - Java - 用于设备控制的非阻塞 SocketChannel,通用方法
- p5.js - p5js中的动画PNG帧速率
- php - 如何更改 GoDaddy 中的名称服务器可能会在托管程序中托管
- jwt - 连接到 Revolut 沙箱
- flask - 我如何知道何时发送了 OIDC 身份验证重定向?
- c# - 如何从 C# MVC .net 核心中的用户名(身份名称)获取 Azure AD 用户组名称?
- zsh - iterm2 标签标题的变量是什么?
- excel - 如何解决 .save 上的 VBA 调试错误?
- ios - 如何使用 Swift 在 iPhone 上点亮屏幕