【问题标题】:Tweepy: Collect tweets with at least 1 word each of two lists of search wordsTweepy:收集两个搜索词列表中至少有 1 个词的推文
【发布时间】:2021-03-03 00:25:11
【问题描述】:

我正在使用 Tweepy 及其光标来收集带有特定搜索词的推文。我的目标是有两个关于两个不同主题的单词列表,例如清单 1 包含有关爱的词语,清单 2 包含有关健康的词语。然后,我想搜索每条推文至少包含列表 1 中的一个单词和列表 2 中的至少一个单词。我的问题是我什至无法运行仅使用一个列表的搜索。

所以我有以下代码:

# extracting words from a csv-file
file_loc1 = "search_words/love.xlsx"
love_words = pd.read_excel(file_loc1, index_col=None, na_values=['NA'], usecols = "A", skiprows=11)
love_words = str(love_words['love'].values)

# converting the list to readable search terms (there are probably more elegant ways...)
love_words = love_words.lower()
love_words = love_words.replace("\r","")
love_words = love_words.replace("\n","")
love_words = love_words.replace("' '", " OR ")
love_words = love_words.replace("[", "")
love_words = love_words.replace("]", "")
love_words = love_words.replace("'", "")

search_words = love_words + " -filter:retweets"
date_since = "2020-01-01"

tweets = tw.Cursor(api.search,
              q=search_words,
              lang="en",
              since=date_since).items(5000)

tweet_text = [tweet.text for tweet in tweets]

所以我从 csv 文件中检索单词并将它们全部放入一个字符串中,最终将如下所示:word1 OR word2 OR word3 -filter:retweets。 如果只有两三个词,它似乎有效,我收到了很多推文。但是如果我使用更多的术语,我不会收到任何推文。似乎 OR 运算符的工作方式不像我认为的那样......最后我希望像(love1 OR love2 OR love3 OR ...) AND (health1 OR health2 OR ...) 这样的搜索,这样我就会得到包含每个单词的一个或多个单词的推文两个列表。

我希望这个解释是有道理的。有什么建议么?谢谢!

【问题讨论】:

    标签: python twitter tweepy


    【解决方案1】:

    我已经实现了 Tweepy,但发现 OR 运算符还不够。我所做的是单独搜索每个关键字并收集所有推文:

    tweet_list = []
    for word in keyword_list:
        tweets = api.search(word)
        tweet_list.append(tweets)
    

    然后,在我获得所有推文后,我会过滤它们是否包含我感兴趣的词。

    这不是有效的,也不太可能是最好的解决方案。但它对我有用。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-01-12
      • 2022-11-12
      • 2017-12-26
      • 1970-01-01
      • 2013-06-14
      • 2014-11-10
      • 1970-01-01
      • 2021-05-01
      相关资源
      最近更新 更多