在 python 中使用过滤词搜索用户的推文

Search user's tweet with filtered word in python

我只想获取过滤后的推文,但出现此错误。我该如何解决?这个错误是什么意思?

tweets = api.user_timeline(screen_name = "elonmusk", count = 200000, lang = "en", tweet_mode = "extended")

word="Dogecoin"
for tweet in tweets:
    if word in tweet:
        df = pd.DataFrame([tweet.full_text], columns = ["tweet"])

TypeError: 'Status' 类型的参数不可迭代

TypeError: argument of type 'Status' is not iterable 表示您正在尝试循环不可循环的内容。

这表明 api.user_timeline() 的 return 值不是推文列表(正如您的代码所期望的那样),而是一个 Status 类型的值。

这里出现错误:

if word in tweet:

这里tweet是一个Status对象,所以不能直接对它使用in运算符。你想检查一个词是否在推文的文本中,所以你需要这样做:

if word in tweet.full_text: