我尝试制作通用python
脚本,通过mailchimp
api检索任意数量的列表。然而,"分页"功能不起作用。我的帐户中有十六个列表,无论我为offset=n&count=n
尝试什么价值,我只得到那16条记录。以下是我获取列表的代码的一部分:
#FETCH ALL REPORTS
baseurl = "https://" + dc + ".api.mailchimp.com/3.0/"
request = urllib2.Request(baseurl + "reports/")
base64string = base64.encodestring('%s:%s' % (username, key)).replace('\n', '')
request.add_header("Authorization", "Basic %s" % base64string)
try:
output = urllib2.urlopen(request).read()
data = json.loads(output)
except:
print "Error occurred. Make sure you entered the correct api key"
exit()
createfile("allcampaigns.json", output)
psize, i = 10, 0
while(True):
list_url = baseurl + "lists" + '?offset=' + str(psize * i) + '&count=' + str(psize)
#list_url = baseurl + "lists+ '?offset=' + str(psize * i) + '&count=' + str(psize)
print list_url
request = urllib2.Request(list_url)
base64string = base64.encodestring('%s:%s' % (username, key)).replace('\n', '')
request.add_header("Authorization", "Basic %s" % base64string)
output = urllib2.urlopen(request).read()
createfile("alllists.json", output)
#print output
data.update(json.loads(output))
cnt = len(data)
print cnt, " lists retrieved."
if cnt<psize: break #cnt could also be zero if no records are returned
i += 1
答案 0 :(得分:1)
问题原来只在我的代码中,当然mailchimp api
没有任何问题。在返回的json
对象上,我计算len(数据)而不是len(data.lists)对象。修复它并开始工作!