首页 > 解决方案 > 使用 bounding_box 收集 Twitter 流

问题描述

我正在使用 tweepy.Cursor 来收集推文,它工作正常,我想改进我的代码,以通过 bounding_box 将推文的收集限制为给定的坐标对。我对python的了解非常有限。我需要帮助来改进此代码。代码如下:

import sys
import os
import jsonpickle
import tweepy
import datetime
import time

consumer_key = 'xxxxxxxxxxxxxxxxxxxxxxxxx'
consumer_secret = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'

auth = tweepy.AppAuthHandler(consumer_key, consumer_secret)
api = tweepy.API(auth, wait_on_rate_limit=True,wait_on_rate_limit_notify=True)

maxTweets = 1000000
tweetCount = 0

stamp = time.strftime('%b%d%y%H%M')
filename = 'AFG'+stamp+'.json'

print(filename)

searchQuery = ('afghanistan OR elections')

with open(filename, 'w') as f:
    for tweet in tweepy.Cursor(api.search, q=searchQuery, tweet_mode='extended').items(maxTweets):
        f.write(jsonpickle.encode(tweet._json, unpicklable=False) + '\n')
        tweetCount += 1

print("Downloaded {0} tweets.". format(tweetCount))

我不知道在上面的代码中将 bounding_box 放在哪里。阿富汗的 Bounding_box 是:'AF': ('Afghanistan', (60.5284298033, 29.318572496, 75.1580277851, 38.4862816432))

标签: pythontweepybounding-box

解决方案


推荐阅读