我有根据https://www.karambelkar.info/2015/01/how-to-use-twitters-search-rest-api-most-effectively./遍历多个Twitter搜索查询REST API
问题拉鸣叫工作REST API搜索脚本:此代码的工作,但拉带searchQuery1
和searchQuery2
鸣叫。 (例如用Prostate Cancer
+ Colon Cancer
推文)。我不想要这个。相反,我想获得来自searchQuery1
(仅包含Prostate Cancer
的推文)和来自searchQuery2
的所有推文(仅包含Colon Cancer
的推文)的所有推文。查询应单独运行。
目标:按顺序循环过的搜索查询的X号(例如searchQuery1
,searchQuery2
等)
谢谢!
searchQuery1 = 'Prostate Cancer'
searchQuery2 = 'Colon Cancer'
maxTweets = 10000
tweetsPerQry = 100
fprefix = 'REST'
sinceId = None
max_id = -1L
tweetCount = 0
with open('/Users/eer/Desktop/' + fprefix + '.' + time.strftime('%Y-%m-%d_%H-%M-%S') + '.json', 'a+') as f: #open file
while tweetCount < maxTweets:
try:
if (max_id <= 0):
if (not sinceId):
for x,y in zip(searchQuery1,searchQuery2):
new_tweets = api.search(q=[searchQuery1, searchQuery2], count=tweetsPerQry)
else:
print "sinceID 1"
new_tweets = api.search(q=[searchQuery1, searchQuery2], count=tweetsPerQry,
since_id=sinceId)
else:
if (not sinceId):
print "not sinceID 2"
new_tweets = api.search(q=[searchQuery1, searchQuery2], count=tweetsPerQry,
max_id=str(max_id - 1))
else:
print "sinceID 1"
new_tweets = api.search(q=[searchQuery1, searchQuery2], count=tweetsPerQry,
max_id=str(max_id - 1),
since_id=sinceId)
if not new_tweets:
print("No more tweets found")
break
for tweet in new_tweets:
f.write(jsonpickle.encode(tweet._json, unpicklable=False) +
'\n')
tweetCount += len(new_tweets)
max_id = new_tweets[-1].id
except tweepy.TweepError as e:
print("some error : " + str(e))
break
print ("Downloaded {0} tweets, Saved to {1}".format(tweetCount, fprefix))
你要想要获得所有的鸣叫在含有上周searchQuery1不包含searchQuery2,然后让所有的鸣叫,在过去一周包含searchQuery2不包含searchQuery1? – Jonas