Specify exact time to start and end the collection of tweets using Python Tweepy?

时光毁灭记忆、已成空白 提交于 2020-01-05 11:38:27

问题


I am having quite a bit of technical issues. My python script below usually works (when time is in yyyy-mm-dd' format. But during the extremely heavy tweet activities, for example more than 500,000 tweets collected a day, my computer runs out of memory and have to force stop the program.

I can work around by looking at the time of the last tweets in the stopped csv file, in this case it's at time 18:44:00. I have tried many time format (for example 'yyyy-mm-dd hh:mm:ss' format as below) but none actually works.

import tweepy
import time
import csv

ckey = ""
csecret = ""
atoken = ""
asecret = ""

OAUTH_KEYS = {'consumer_key':ckey, 'consumer_secret':csecret,
    'access_token_key':atoken, 'access_token_secret':asecret}
auth = tweepy.OAuthHandler(OAUTH_KEYS['consumer_key'], OAUTH_KEYS['consumer_secret'])
api = tweepy.API(auth)

# Stream the first "xxx" tweets related to "car", then filter out the ones without geo-enabled
# Reference of search (q) operator: https://dev.twitter.com/rest/public/search

# Common parameters: Changeable only here
startSince = '2014-09-18 00:00:00'
endUntil = '2014-09-18 18:44:00'
suffix = '_18SEP2014.csv'

############################
### Lung cancer starts #####
searchTerms2 = '"lung cancer" OR "lung cancers" OR "lungcancer" OR "lungcancers" OR \
    "lung tumor" OR "lungtumor" OR "lung tumors" OR "lungtumors" OR "lung neoplasm"'

# Items from 0 to 500,000 (which *should* cover all tweets)
# Increase by 4,000 for each cycle (because 5000-6000 is over the Twitter rate limit)
# Then wait for 20 min before next request (becaues twitter request wait time is 15min)

counter2 = 0
for tweet in tweepy.Cursor(api.search, q=searchTerms2, 
    since=startSince, until=endUntil).items(999999999): # changeable here

    try:
        '''
        print "Name:", tweet.author.name.encode('utf8')
        print "Screen-name:", tweet.author.screen_name.encode('utf8')
        print "Tweet created:", tweet.created_at'''

        placeHolder = []
        placeHolder.append(tweet.author.name.encode('utf8'))
        placeHolder.append(tweet.author.screen_name.encode('utf8'))
        placeHolder.append(tweet.created_at)

        prefix = 'TweetData_lungCancer'
        wholeFileName = prefix + suffix     
        with open(wholeFileName, "ab") as f: # changeable here
            writeFile = csv.writer(f)
            writeFile.writerow(placeHolder)

        counter2 += 1

        if counter2 == 4000:
            time.sleep(60*20) # wait for 20 min everytime 4,000 tweets are extracted 
            counter2 = 0
            continue

    except tweepy.TweepError:
        time.sleep(60*20)
        continue

    except IOError:
        time.sleep(60*2.5)
        continue

    except StopIteration:
        break

来源:https://stackoverflow.com/questions/26190946/specify-exact-time-to-start-and-end-the-collection-of-tweets-using-python-tweepy

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!