最终目标是使用tweepy api搜索专注于主题(即docker)和EXCLUDE转发。我已经看过提及不包括转推的其他主题,但它们完全适用。我试图将我学到的知识融入到下面的代码中,但我相信“如果不是”代码片段在错误的地方。任何帮助是极大的赞赏。
#!/usr/bin/python
import tweepy
import csv #Import csv
import os
# Consumer keys and access tokens, used for OAuth
consumer_key = 'MINE'
consumer_secret = 'MINE'
access_token = 'MINE'
access_token_secret = 'MINE'
# OAuth process, using the keys and tokens
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
api = tweepy.API(auth)
# Open/Create a file to append data
csvFile = open('docker1.csv', 'a')
#Use csv Writer
csvWriter = csv.writer(csvFile)
ids = set()
for tweet in tweepy.Cursor(api.search,
q="docker",
Since="2016-08-09",
#until="2014-02-15",
lang="en").items(5000000):
if not tweet['retweeted'] and 'RT @' not in tweet['text']:
#Write a row to the csv file/ I use encode utf-8
csvWriter.writerow([tweet.created_at, tweet.text.encode('utf-8'), tweet.favorite_count, tweet.retweet_count, tweet.id, tweet.user.screen_name])
#print "...%s tweets downloaded so far" % (len(tweet.id))
ids.add(tweet.id) # add new id
print ("number of unique ids seen so far: {}",format(len(ids)))
csvFile.close()
答案 0 :(得分:16)
因此tweet
是一个不是JSON或dict的对象,您不应该像tweet['retweeted']
和tweet['text']
而是使用这一行:
if not tweet.retweeted:
或者您的用例:
if (not tweet.retweeted) and ('RT @' not in tweet.text):
在API级别过滤:
q='your_search -filter:retweets'
详细了解here。