如何在VKontakte和Python中使用offset?

2024-07-07 07:40:31 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试构建一个脚本,在那里我可以获得特定位置的签入。由于某些原因,当我指定lat时,long coords VK从不返回任何签入,所以我必须首先获取位置id,然后从该列表中请求签入。但是,我不确定如何使用offset特性,我认为它的工作方式有点像分页函数。在

到目前为止,我有这个:

import vk
import json


app_id =   #enter app id 
login_nr =    #enter your login phone or email
password = ''   #enter password  
vkapi = vk.API(app_id, login_nr, password)

vkapi.getServerTime()

def get_places(lat, lon, rad):

    name_list = []
    try:
        locations = vkapi.places.search(latitude=lat, longitude=lon, radius=rad)
        name_list.append(locations['items'])
    except Exception, e:
        print '*********------------ ERROR ------------*********'
        print str(e)

    return name_list


 # Returns last checkins up to a maximum of 100
 # Define the number of checkins you want, 100 being maximum

def get_checkins_id(place_id,check_count):

    checkin_list= []

    try:
        checkins = vkapi.places.getCheckins(place = place_id, count = check_count)
        checkin_list.append(checkins['items'])
    except Exception, e:
        print '*********------------ ERROR ------------*********'
        print str(e)

    return checkin_list

我最终想做的是将这两者结合成一个函数,但在此之前,我必须弄清楚offset是如何工作的,当前的vkapi文档并没有很好地解释这一点。我希望代码读起来类似于:

^{pr2}$

据我所知,我必须计算一下签入时的偏移量,然后以某种方式说明有100多个签入的位置。无论如何,我将非常感谢任何类型的帮助,建议,或任何东西。如果你对剧本有什么建议,我也很乐意听听。我自己显然不是很好。在

谢谢!在


Tags: nameidappcountloginplacepasswordlist
2条回答

我曾经用javascript处理过vkapi,但我认为,逻辑是一样的。在

TL;DR:Offset是许多结果(从第一个开始),API应该跳过这些结果来响应

例如,您进行查询,它应该返回1000个结果(假设您知道确切的结果数)。 但VK每次只给你100次。那么,如何获得另外900美元呢?在

你对API说:给我next100个结果。下一个是offset-由于已经处理过的结果而要跳过的数量。所以,vkapi接受1000个结果,跳过前100个,然后返回到下一个(第二个)100个。在

另外,如果您在第一段中讨论这个方法(http://vk.com/dev/places.getCheckins),请检查您的lat/long是否为float,而不是integer。尝试交换经纬度/经度可能有用-也许你把它们弄混了?在

相关问题 更多 >