【问题标题】:Tweepy - Limiting certain tweetsTweepy - 限制某些推文
【发布时间】:2017-07-16 00:06:15
【问题描述】:

我正在尝试获取以下代码以从列表中排除任何包含受限单词的推文。最好的方法是什么?

一旦我跳出流,这段代码也只返回最后一条推文。有没有办法将所有适用的推文打印到 CSV?

import sys
import tweepy
import csv

#pass security information to variables
consumer_key = ''
consumer_secret = ''
access_key = ''
access_secret = ''


#use variables to access twitter
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_key, access_secret)
api = tweepy.API(auth)

#create an object called 'customStreamListener'

class CustomStreamListener(tweepy.StreamListener):

    def on_status(self, status):
        print (status.author.screen_name, status.created_at, status.text)
        # Writing status data
        with open('OutputStreaming.csv', 'w') as f:
            writer = csv.writer(f)
            writer.writerow([status.author.screen_name, status.created_at, status.text])


    def on_error(self, status_code):
        print >> sys.stderr, 'Encountered error with status code:', status_code
        return True # Don't kill the stream

    def on_timeout(self):
        print >> sys.stderr, 'Timeout...'
        return True # Don't kill the stream

# Writing csv titles
with open('OutputStreaming.csv', 'w') as f:
        writer = csv.writer(f)
        writer.writerow(['Author', 'Date', 'Text'])

streamingAPI = tweepy.streaming.Stream(auth, CustomStreamListener())
streamingAPI.filter(track=['Hasbro', 'Mattel', 'Lego'])

【问题讨论】:

  • 问题解决了吗?
  • 还在破解吗?
  • 我觉得我在这里很好。我让它运行,但过滤可能我需要使用不同的包进一步研究。

标签: python csv twitter tweepy


【解决方案1】:

Twitter API 中的documentation for the track parameter 表示无法从过滤器中排除术语,只能包含单词和短语。您必须在代码中实现一个额外的过滤器,以丢弃包含您不想包含在结果集中的单词的推文。

【讨论】:

  • 我想这将是我真正的问题。在上面的实际代码中实现这一点的最佳方法是什么?
  • 在 on_status 内部实现检查 status.text,如果文本包含您不感兴趣的单词,则在写入文件之前提前中断。
  • 谢谢,我会看看这个
【解决方案2】:

无法从过滤器功能中排除术语,但您可以实现自定义选择。 基本上这个想法是检查推文的单词是否包含不允许的单词。 您可以使用 nltk 模块简单地标记推文的文本。

来自nltk主页的一个简单例子:

>>> import nltk
>>> sentence = """At eight o'clock on Thursday morning
... Arthur didn't feel very good."""
>>> tokens = nltk.word_tokenize(sentence)
>>> tokens
['At', 'eight', "o'clock", 'on', 'Thursday', 'morning',
'Arthur', 'did', "n't", 'feel', 'very', 'good', '.']

不经意间,在您的情况下,sentence 是 tweet.text。 因此,将您的代码更改为类似于以下内容:

def on_status(self, status):
    print (status.author.screen_name, status.created_at, status.text)
    is_allowed = True
    banned_words = ['word_1', 'word2', 'another_bad_word']
    words_text = nltk.word_tokenize(status.text)

    # loop banned_words and search if item is in words_text
    for word in banned_words:
        if word in words_text:
            # discard this tweet
            is_allowed = False
            break

    if is_allowed is True:
        # stuff for writing status data
        # ...

此代码未经测试,但向您展示了实现目标的方法。

告诉我

【讨论】:

  • 不熟悉如何使用 NLTK,但是有没有比文档更容易解释如何使用的地方。
猜你喜欢
  • 2016-05-28
  • 2021-01-16
  • 2018-02-25
  • 2012-07-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多