如何使用Python提取1000条推文?

时间:2019-02-21 20:06:18

标签: python twitter tweepy

我正在尝试根据国家/地区名称提取推文,但是代码始终会检索少量推文(大约23、50和70,不超过此数量)。有人知道如何检索(1000-5000)附近的推文吗?

# this is not my real credentials 
Consume:
CONSUMER_KEY    = ‘xxx’
CONSUMER_SECRET = ‘ttt’

# Access:
ACCESS_TOKEN  = ‘rffg’
ACCESS_SECRET = ‘mmvvvt’

import tweepy
import csv

# get authorization
auth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET)
auth.set_access_token(ACCESS_TOKEN, ACCESS_SECRET)
api = tweepy.API(auth)

# get tweets from country
place = api.geo_search(query="Saudi Arabia", granularity="country" ,since= '10')
place_id = place[0].id

# print tweets and save to csv file
with open('tweets.csv', 'w', newline='', encoding='utf-8') as csvFile:
    tweetWriter = csv.writer(csvFile, delimiter=',')
    tweets = api.search(q='place:%s' % place_id, count=100, since='1')
    count = 0
    for tweet in tweets:
        count += 1
        # tweet.id = unique id for tweet, text = text, place.name = where it was posted, created_at = UTC time
        tweetData = [tweet.id, tweet.user.name, tweet.text, tweet.place.name, tweet.created_at]
        tweetWriter.writerow(tweetData)

    print(count)

0 个答案:

没有答案