在python中提取有限数量的推文

问题描述 投票:-1回答:1
import tweepy

from tweepy.streaming import StreamListener

from tweepy import OAuthHandler

from tweepy import Stream

import json



access_token = ""

access_token_secret = ""

consumer_key = ""

consumer_secret = ""



class StdOutListener(StreamListener):

    def on_data(self, data):       

        try:

            with open('anusri.json', 'a') as f:

                f.write(data)

                return True

         except BaseException as e:

                print("Error on_data: %s" % str(e))
         return True

    def on_error(self, status):

        print (status)

if __name__ == '__main__':


    l = StdOutListener()

    auth = OAuthHandler(consumer_key, consumer_secret)

    auth.set_access_token(access_token, access_token_secret)

    stream = Stream(auth, l)

    api = tweepy.API(auth)

    places = api.geo_search(query="IND", granularity="country")

    place_id = places[0].id

    tweets = api.search(q="place:%s" % place_id)

for tweet in tweets: 

    stream.filter(track=['modi'])

我如何将推文限制为100

python request tweepy
1个回答
0
投票

试试这个将'每页返回'设置为100:

tweets = api.search(q="place:%s" % place_id, rpp=100)
© www.soinside.com 2019 - 2024. All rights reserved.