tweety twitter APi什么都不返回(python)

时间:2016-04-01 18:40:46

标签: python api twitter tweepy

我正在使用tweepy和geocode包将ZipCodes转换为lat和long然后使用tweepy从twitter API中提取但是我没有得到任何回报。我经历过并逐行执行我的代码,并且每次都没有返回任何内容时卡在api.search上。

query = 'stack'
radius = 1000
DataSet = pd.DataFrame
loopCount = 0
appended_data = []
appendData = []

def toDataFrame(tweets):

    DataSet = pd.DataFrame()

    DataSet['tweetID'] = [tweet.id for tweet in tweets]
    DataSet['tweetText'] = [tweet.text for tweet in tweets]
    DataSet['tweetRetweetCt'] = [tweet.retweet_count for tweet in tweets]
    DataSet['tweetFavoriteCt'] = [tweet.favorite_count for tweet in tweets]
    DataSet['tweetSource'] = [tweet.source for tweet in tweets]
    DataSet['tweetCreated'] = [tweet.created_at for tweet in tweets]
    DataSet['userID'] = [tweet.user.id for tweet in tweets]
    DataSet['userScreen'] = [tweet.user.screen_name for tweet in tweets]
    DataSet['userName'] = [tweet.user.name for tweet in tweets]
    DataSet['userCreateDt'] = [tweet.user.created_at for tweet in tweets]
    DataSet['userDesc'] = [tweet.user.description for tweet in tweets]
    DataSet['userFollowerCt'] = [tweet.user.followers_count for tweet in tweets]
    DataSet['userFriendsCt'] = [tweet.user.friends_count for tweet in tweets]
    DataSet['userLocation'] = [tweet.user.location for tweet in tweets]
    DataSet['userTimezone'] = [tweet.user.time_zone for twee

def location(zip):
    geolocator = Nominatim()
    location = geolocator.geocode(zip)
    cordinates = ((location.latitude, location.longitude))
    cordinates = str(cordinates)
    cordinates = cordinates.replace("(","")
    cordinates = cordinates.replace(")","")
    return cordinates

def lookUp(results):
    for result in results:
        DataSet = pd.DataFrame(results)
        print DataSet
        return DataSet

##hidden for SO
auth = tp.OAuthHandler('','')
auth.set_access_token('', '')

api = tp.API(auth)

for zip in zips:
#for row, zip in zips.iterrows():
    if (loopCount == 15):
        t.sleep(960)
        loopCount = 0
    loopCount = loopCount + 1
    cordinates = location(zip)
    inputCode = cordinates + ', ' + str(radius)
    results = api.search(geocode=inputCode, count=100, q=query)
    DataSet = lookUp(results)
    appendData.append(DataSet)
appended_data = pd.concat(appendedData, axis=1)

1 个答案:

答案 0 :(得分:0)

注意不要在geocode中传递空格,也要添加单位。例如,使用您的函数location

In [5]:
zip = 28039
cordinates = location(zip)

In [23]:
radius = '1km'
inputCode = cordinates + ', ' + str(radius)
inputCode = inputCode.replace(' ', '')
inputCode

Out[23]:
'40.4604043354592,-3.70401484102134,1km'

In [24]:
query = 'a'
results = api.search(geocode=inputCode, count=100, q=query)

In [25]:
len(results)
Out[25]:
100

参考twitter docs

  

参数值由“纬度,经度,半径”指定,其中   半径单位必须指定为“mi”(英里)或“km”   (公里)。

希望它有所帮助。