在python中提取有限数量的推文
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了在python中提取有限数量的推文相关的知识,希望对你有一定的参考价值。
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
答案
试试这个将'每页返回'设置为100:
tweets = api.search(q="place:%s" % place_id, rpp=100)
以上是关于在python中提取有限数量的推文的主要内容,如果未能解决你的问题,请参考以下文章