【发布时间】:2021-12-31 12:57:09
【问题描述】:
我重新启动了我几年前停止做的项目。通过一些研究,我使该项目与更新的 API 版本兼容,并且没有问题。我 3 周前工作的代码现在不工作了。首先,我从 include_rts 和 since 命令中得到了一个意外的参数错误,经过一些研究,我将 since 更改为 until 并修复了它。当我从代码中删除包含 rts 并更改了我的关键字(我将它们添加到 -is=retweet)并且代码没有给出错误时,但没有创建我需要从中导入数据的 csv 文件。这是我认为可能是问题的代码
def write_tweets(keyword, file):
if os.path.exists(file):
df = pd.read_csv(file, header=0)
else:
df = pd.DataFrame(columns=COLS)
for page in tweepy.Cursor(api.search_tweets, q=keyword, count=200, include_rts=False, until='2021-12-26').pages(1):
for status in page:
new_entry = []
status = status._json
single_tweet_df = pd.DataFrame([new_entry], columns=COLS)
df = df.append(single_tweet_df, ignore_index=True)
csvFile = open(file, 'a' ,encoding='utf-8')
df.to_csv(csvFile, mode='a', columns=COLS, index=False, encoding="utf-8")
elective_course_keywords = '#Parallel_Programming -is=retweet OR #Cloud_Computing -is=retweet Or #computer_science -is=retweet OR #machine_learning -is=retweet'
elective_course_tweets = "C:/Users/User/Desktop/TwitterDatas/elective_course_tweets_data.csv"
write_tweets(elective_course_keywords, elective_course_tweets)
提前感谢您的帮助
【问题讨论】: