如何解决我应该如何清理从 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'
也尝试添加该行:
if 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,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,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 文件以查看我自己遇到的问题。请让我知道我应该如何清理这些推文。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。