首页 > 解决方案 > 尝试使用 Tweepy 检索 Twitter 地理位置时出错

问题描述

我正在尝试检索地理定位数据并将其放入 ArcGIS 要素类中进行分析,但不幸的是我一直收到错误消息。下面是我的代码。

from tweepy import Stream
from tweepy import OAuthHandler
from tweepy.streaming import StreamListener
import arcpy
import sys  #new
import time  #new

#global variables
consumer_key = 'x9jRE3KQm1LlEFcHsL6bP4TRa'
consumer_secret = '8VVzPzY0DJbbgbBk5bgWCrBADzLEqdnATNbw1z0LUWF5MWuu4g'
token_key = '2997753385-nCFmNPAo2LOt7LLF311Kw0JdsAhcNSq8yQThxtO'
token_secret = '0Dck37JE7HV56Rs5t5GUkbW3C61qepG4fi070RiP4SNdm'

start_time = time.time()
arcpy.env.workspace = r'c:\ArcGIS_Blueprint_Python\data\Twitter\TweetInformation.gdb'

class StdOutListener(StreamListener):
    def __init__(self, start_time, featureClass, time_limit):
        super(StdOutListener, self).__init__()
        self.time = start_time
        self.limit = time_limit
        self.featureClass = featureClass
        
    def on_status(self, status):
        while (time.time() - self.time) < self.limit:
            if status.geo is not None:
                dictCoords = status.geo
                listCoords = dictCoords['coordinates']
                latitude = listCoords[0]
                longitude = listCoords[1]

                cursor = arcpy.da.InsertCursor(self.featureClass,("SHAPE@XY"))
                cursor.insertRow([(longitude,latitude)])

                print(str(listCoords[0]) + "," + str(listCoords[1]))
                return True
            else:
                #print "No coordinates found"
                return True
        exit()
       
    def on_error(self, status):
        print('Error...')
        print status
        return True
    
    def on_timeout(self):
        print('Timeout...')
        return True
    

#Main function
def main():
    try:  #new
        featureClass = sys.argv[1]
        monitorTime = sys.argv[2]
        monitorTime = monitorTime * 3600
       
        sr = arcpy.SpatialReference(4326)
        arcpy.env.overwriteOutput = True
        arcpy.CreateFeatureclass_management(arcpy.env.workspace, featureClass, "POINT", spatial_reference=sr)

        auth = OAuthHandler(consumer_key, consumer_secret)
        auth.set_access_token(token_key, token_secret)
    
        stream = Stream(auth, StdOutListener(start_time, featureClass, time_limit=monitorTime))  #172800
        #stream.filter(track=['#wildfire', '#forestfire', '#northstarfire', '#tunkblockfire', '#roughfire','#happycampfire'])
        stream.filter(track=['#SEC', '#SECFootball', '#RollTide', '#GigEm', '#Bama', '#UGABulldogs','#Dawgs', '#GeorgiaBulldogs', '##A&MFootball', '#KyleField', '#Aggies', '#gigem','#LSUFootball','#LSUFB', '#WarEagle','#AuburnFootball' ])
    except Exception as e:  #new
        print(e.message)  #new

if __name__ == '__main__':
    main()
Traceback (most recent call last):
  File "C:\Users\18164\Desktop\Chapter (9)\tweepy_stream.py", line 55, in main
    featureClass = sys.argv[1] IndexError: list index out of range

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Users\18164\Desktop\Chapter (9)\tweepy_stream.py", line 73, in <module>
    main()
  File "C:\Users\18164\Desktop\Chapter (9)\tweepy_stream.py", line 70, in main
    print(e.message)  #new AttributeError: 'IndexError' object has no attribute 'message'

标签: pythontweepyarcgis

解决方案


首先,如果您还没有重新生成 Twitter API 凭据,那么您现在需要重新生成。

如前所述,错误表明 的长度sys.argv为 1 并且没有传递命令行参数。


推荐阅读