简体   繁体   English

如何使用Mailchimp API 3.0从用户或兴趣中获得10个以上的条目?

[英]How do I get more than 10 entries from Users or Interests using Mailchimp API 3.0?

I have a few lists in mailchimp, some which have thousands of users and one of the representatives recommended I merge some of my lists and use 'groups' (AKA Interests) to target certain audiences. 我在mailchimp中有一些列表,其中一些有成千上万的用户,其中一位代表建议我合并我的一些列表并使用“群组”(AKA兴趣)来定位某些受众。

I have one 'interest-category' that contains over 40 separate interests, and I want to get their IDs along side the names so I can subscribe users through the API and add them to the right 'groups/interests'. 我有一个包含40多个不同兴趣的“兴趣类别”,我想在名称旁边获取他们的ID,这样我就可以通过API订阅用户并将其添加到正确的“群组/兴趣”中。

I am so close to getting the interests along with their names, but the documentation does not say anything about increasing the amount of entries or going to the next 10. http://kb.mailchimp.com/api/resources/lists/interest-categories/interests/lists-interests-collection 我非常接近获得他们的兴趣以及他们的名字,但文档没有说明增加条目数量或进入下一个10。http://kb.mailchimp.com/api/resources/lists/interest -类别/权益/列表-利益收集

If this helps at all, this is the code I used to pull up the interests. 如果这有帮助的话,这就是我用来提高兴趣的代码。 (Written in Python and uses the 'requests' library) (用Python编写并使用'requests'库)

import requests
print "Name\tID"
r = requests.auth.HTTPBasicAuth('x', '00000000000000000000000000000-us4')
interest_categories_raw = requests.get('http://us4.api.mailchimp.com/3.0/lists/0000000000/interest-categories/', auth=r)
interest_categories = json.loads(interest_categories_raw.content)
for category in interest_categories['categories']:
    url = 'http://us4.api.mailchimp.com/3.0/lists/0000000000/interest-categories/{0}/interests'.format(str(category['id']))
    interests = json.loads(requests.get(url, auth=r).content)
    for interest in interests['interests']:
        print "{0}\t{1}".format(interest['name'],interest['id'])

Is there any way I can get the rest of the 'interests/groups', both the Name and the ID so I can assign them correctly? 有没有什么方法可以获得其余的'兴趣/群组',包括名称和ID,以便我可以正确分配它们? (Any way other than uploading test users and patching their interest data and checking the website to see what groups they are added into) (除了上传测试用户和修补他们的兴趣数据以及检查网站以查看他们被添加到哪个组之外的任何方式)

In TooMuchPete's comment, he mentioned that the getting started document mentions pagination. 在TooMuchPete的评论中,他提到入门文件提到了分页。 Mailchimp's API uses 'count' and 'offset' for their pagination. Mailchimp的API使用'count'和'offset'作为分页。 They are parameters added to the end of the URL. 它们是添加到URL末尾的参数。 Count is the amount of items per page and Offset is how far off from the page will start (offset=0 will start on the first entry). Count是每页的项目数量,Offset是从页面开始的距离(offset = 0将从第一个条目开始)。 So the url 所以网址

https://us4.api.mailchimp.com/3.0/lists/XXXXXXXXXX/members/?count=5&offset=10

will get 5 items starting on the 11th item ("3rd page", items 11 - 16) 将从第11项开始获得5项(“第3页”,项目11-16)

Before TooMuchPete's comment, I answered my own question with this: 在TooMuchPete的评论之前,我回答了我自己的问题:

I found what I was looking for in terms of the Interest IDs matched with their name. 我找到了我想要的兴趣ID与他们的名字相匹配。 Rather than looking for the IDs by category, the user object already has all of the interests IDs in one array, I just used the IDs to find their names going backwards. 用户对象已经将所有兴趣ID都放在一个数组中,而不是按类别查找ID,我只是使用ID来查找它们的名称。 For anyone who might have the same problem in the future, this is the code snippet I wrote to find the Names with the IDs: 对于将来可能遇到同样问题的任何人,这是我写的用于查找带有ID的名称的代码片段:

import requests
print "ID\tName"
r = requests.auth.HTTPBasicAuth('x', '00000000000000000000000000000000-us4')
interest_categories_raw = requests.get('http://us4.api.mailchimp.com/3.0/lists/0000000000/interest-categories/', auth=r)
interest_categories = json.loads(interest_categories_raw.content)

# List of all the interest IDs. Can be found from any user
# https://us4.api.mailchimp.com/3.0/lists/0000000000/members/000000000000000000000000
interest_ids = ["00000000000", "0000000000"]

for i_id in interest_ids:
    for category in interest_categories['categories']:
        url = 'http://us4.api.mailchimp.com/3.0/lists/0000000000/interest-categories/{0}/interests/{1}'.format(str(category['id']), str(i_id))
        raw_response = requests.get(url, auth=r)
        if raw_response.status_code != 404:
            interest = json.loads(raw_response.content)
            print interest['id'] + "\t" + interest['name']

This code will find all the Names and match them to all the IDs of the list. 此代码将查找所有名称,并将它们与列表的所有ID匹配。 If any one else finds a better way to match names to IDs, please add that to this answer. 如果其他任何人找到更好的方法将名称与ID匹配,请将其添加到此答案中。

You should use /?count=&offset= parameters, but in order to paginate, the offset must increment not by +1 , but +count , because its the offset not the page. 您应该使用/?count=&offset=参数,但为了分页,偏移量必须不增加+1 ,而是+count ,因为它的偏移量不是页面。

So ?count=50&offset=50 gives you 51-100 (2nd page) 那么?count=50&offset=50给你51-100 (第2页)

I was having a similar problem with only 10 interests being loaded per interest-category, but buried in the documentation (under pagination, api version 3.0) is that by default the "count" is 10. I simply changed the count parameter to a larger number and all interests were returned without having to use a looping mechanism to pull all records. 我遇到了类似的问题,每个兴趣类别只加载10个兴趣,但是埋在文档中 (在分页下,api版本3.0)是默认情况下“count”是10.我只是将count参数更改为更大返回数字和所有兴趣,而不必使用循环机制来提取所有记录。

https://usX.api.mailchimp.com/3.0/campaigns?offset=0& count=37 https://usX.api.mailchimp.com/3.0/campaigns?offset=0&count = 37

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM