使用 python 从 mailchimp 的特定列表中检索订阅者的所有电子邮件地址
using python to retrieve all email address of subscribers from a specific list in mailchimp
我正在尝试从 Mailchimp 中的特定列表(具有唯一列表 ID)中获取订阅者的所有电子邮件地址。
如果我打印 body,输出格式如下 json。
我正在尝试将 json 转换为字典。
转换为字典后,我想得到所有的email_address。
在我得到所有的电子邮件地址后,我想用 md5 加密它。
然而我运行进入错误'TypeError: expected string or buffer'。
我真的是python的新手,尝试解决它但无法解决。感谢您查看我的问题。
/* My python code */
params = {
'apikey': 'xyz',
'listId': 'abc' }
config = MailChimpConfig()
endpoint = "https://us5.api.mailchimp.com/3.0/lists/'listId'/members?
apikey='apikey'&status=subscribed"
while True:
response = requests.get(endpoint, auth=('apikey', config.apikey),
params=params, verify=False)
try:
response.raise_for_status()
body = response.json
dict = json.loads(body)
print(dict.members[0].email_address)
break
except requests.exceptions.HTTPError as err:
print "Error: {} {}".format(str(response.status_code), err)
print json.dumps(response.json(), indent=4)
break
except ValueError:
print "Cannot decode json, got %s" % response.text
break
/* end of my python code */
/* If I print body, the output is in json format as below:*/
{
- members: [
- {
id: "",
email_address: "x@hotmail.com",
etc:""
},
- {
id: "",
email_address: "y@gmail.com",
etc:""
}
/* end of json format */
这是不对的:
body = response.json
dict = json.loads(body)
response.json
不是 JSON 对象或 str
,它是一个函数。调用时,它 returns 一个 Python 对象,表示来自响应的 JSON.
的数据
试试这个:
# UNTESTED
# Interpret the JSON string:
data = response.json()
# Print one of the email addresses:
print(data['members'][0]['email_address'])
# Print all of the email addresses
addresses = [x['email_address'] for x in data['members']]
print(addresses)
一旦你有了地址列表,你就可以这样打印每个地址的MD5摘要:
# UNTESTED
for address in addresses:
print(hashlib.md5(address.encode('utf-8')).hexdigest())
如果您想打印一个代表所有地址的 MD5:
# UNTESTED
md5 = hashlib.md5()
for address in sorted(addresses):
md5.update(address.encode('utf-8'))
print(md5.hexdigest())
我正在尝试从 Mailchimp 中的特定列表(具有唯一列表 ID)中获取订阅者的所有电子邮件地址。
如果我打印 body,输出格式如下 json。
我正在尝试将 json 转换为字典。
转换为字典后,我想得到所有的email_address。
在我得到所有的电子邮件地址后,我想用 md5 加密它。
然而我运行进入错误'TypeError: expected string or buffer'。
我真的是python的新手,尝试解决它但无法解决。感谢您查看我的问题。
/* My python code */
params = {
'apikey': 'xyz',
'listId': 'abc' }
config = MailChimpConfig()
endpoint = "https://us5.api.mailchimp.com/3.0/lists/'listId'/members?
apikey='apikey'&status=subscribed"
while True:
response = requests.get(endpoint, auth=('apikey', config.apikey),
params=params, verify=False)
try:
response.raise_for_status()
body = response.json
dict = json.loads(body)
print(dict.members[0].email_address)
break
except requests.exceptions.HTTPError as err:
print "Error: {} {}".format(str(response.status_code), err)
print json.dumps(response.json(), indent=4)
break
except ValueError:
print "Cannot decode json, got %s" % response.text
break
/* end of my python code */
/* If I print body, the output is in json format as below:*/
{
- members: [
- {
id: "",
email_address: "x@hotmail.com",
etc:""
},
- {
id: "",
email_address: "y@gmail.com",
etc:""
}
/* end of json format */
这是不对的:
body = response.json
dict = json.loads(body)
response.json
不是 JSON 对象或 str
,它是一个函数。调用时,它 returns 一个 Python 对象,表示来自响应的 JSON.
试试这个:
# UNTESTED
# Interpret the JSON string:
data = response.json()
# Print one of the email addresses:
print(data['members'][0]['email_address'])
# Print all of the email addresses
addresses = [x['email_address'] for x in data['members']]
print(addresses)
一旦你有了地址列表,你就可以这样打印每个地址的MD5摘要:
# UNTESTED
for address in addresses:
print(hashlib.md5(address.encode('utf-8')).hexdigest())
如果您想打印一个代表所有地址的 MD5:
# UNTESTED
md5 = hashlib.md5()
for address in sorted(addresses):
md5.update(address.encode('utf-8'))
print(md5.hexdigest())