【问题标题】:Tweepy and csv error: unexpected parameter for include_rts and import json to csvTweepy 和 csv 错误:include_rts 的意外参数并将 json 导入 csv
【发布时间】: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)

提前感谢您的帮助

【问题讨论】:

    标签: python json csv tweepy


    【解决方案1】:

    我会假设这完全是关于 Tweepy 库的更改版本。您的代码很可能适用于 3.10 或更低版本并且在该版本中工作。自 4.0 版以来,Tweepy 发生了重大的内部变化。为了不大量重做代码,你可以简单地将Tweepy版本强制降级到3.10,然后旧代码将再次工作。我的类似情况就这样解决了。

    【讨论】:

      猜你喜欢
      • 2022-01-04
      • 1970-01-01
      • 2016-08-03
      • 2015-11-21
      • 2023-03-15
      • 2011-09-06
      • 2016-07-10
      • 1970-01-01
      • 2014-12-28
      相关资源
      最近更新 更多