使用tweepy删除推文时如何排除转发

时间:2020-07-10 11:11:39

标签: python twitter tweepy

我想用tweepy从Twitter上删除数据,但我不希望将转发添加到其中。怎么做?

这是我的代码:

import tweepy
import csv

consumer_key = 'xx'
consumer_secret = 'xx'
access_token = 'xx'
access_token_secret = 'xx'

auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
api = tweepy.API(auth,wait_on_rate_limit=True)

csvpefile = open ('netflix.csv', 'a')
csvWriter = csv.writer(csvpefile)


for tweet in tweepy.Cursor(api.search,
                       q=["netflix"], 
                       lang="id",
                       since="2020-07-8", 
                       tweet_mode = 'extended', 
                       trucated='false').items(200):

print(tweet.created_at, tweet.id, tweet.full_text)
csvWriter.writerow([tweet.created_at, tweet.id, tweet.full_text.encode('utf-8')])

我尝试放

                    count=None,
                    since_id=None,
                    max_id=None,
                    trim_user=False,
                    exclude_replies=False,
                    contributor_details=False,
                    include_entities=True):
                  

q=["netflix -filter:retweets"]

在api.search中,但仍然无法正常工作

2 个答案:

答案 0 :(得分:1)

通过添加快速检查以查看tweet文本字符串是否以rt @开头的方法,您应该能够阻止转推出现在csv文档中。

import tweepy
import csv

consumer_key = 'xx'
consumer_secret = 'xx'
access_token = 'xx'
access_token_secret = 'xx'

auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
api = tweepy.API(auth,wait_on_rate_limit=True)

csvpefile = open ('netflix.csv', 'a')
csvWriter = csv.writer(csvpefile)


for tweet in tweepy.Cursor(api.search,
                       q=["netflix"], 
                       lang="id",
                       since="2020-07-8", 
                       tweet_mode = 'extended', 
                       trucated='false').items(200):

# could go without this variable it just makes it easier
tweettext = str(tweet.full_text.lower().encode('ascii',errors='ignore'))

# check if the tweet starts with the format for a retweet
if tweettext.startswith("rt @") == False:
    csvWriter.writerow([tweet.created_at, tweet.id, tweet.full_text.encode('utf-8')])

答案 1 :(得分:0)

要在搜索中排除转发,请尝试:

q=["netflix -filter:retweets"]

注意:使用tweepy并不是“抓取”。您正在使用公共API。