使用 SendGrid API 从 Json 请求中获取特定列

Get Specific column from Json Request using SendGrid APIs

我正在使用 SendGrid API 来获取我的电子邮件状态。

https://api.sendgrid.com/api/bounces.get.json?api_user=your_sendgrid_username&api_key=your_sendgrid_password&date=1

我已经编写了这段 python 代码来获取请求的数据...

import requests

SG_USERNAME = 'abc'
SG_PASSWORD = 'abc'
SG_URL = "https://sendgrid.com/api"

if __name__ == '__main__':
     block_url = "{}/bounces.get.json?api_user=abc&api_key=abc".format(
        SG_URL, SG_USERNAME, SG_PASSWORD)
     res = requests.get(block_url)
     # Example output:
     # [{u'status': u'4.0.0', u'reason': u'Unable to resolve MX host example.com: noerror', u'email': u'test@example.com'}]
     print block_url
     print res.json()

请求 Return 像这样的请求...但我必须从中提取所有 returns 电子邮件。我将如何使用上面的 python 代码来做到这一点。

     [
{
"status": "5.1.1",
"reason": "550 5.1.1 The email account that you tried to reach does not exist. Please try double-checking the recipient's email address for typos or unnecessary spaces. Learn more at http://support.google.com/mail/bin/answer.py?answer=6596 qb4si4640453igb.5 - gsmtp ",
"email": "taqi.hass@cogilent.com"
},
{
"status": "5.1.1",
"reason": "550 5.1.1 The email account that you tried to reach does not exist. Please try double-checking the recipient's email address for typos or unnecessary spaces. Learn more at http://support.google.com/mail/bin/answer.py?answer=6596 l81si30386677iod.48 - gsmtp ",
"email": "taqi.offfffffffffffff@gmail.com"
}
]

res.json是一个字典列表。然后,您可以使用列表推导来 select 每个字典的所有 "email":

>>> [d["email"] for d in res.json()]
['taqi.hass@cogilent.com', 'taqi.offfffffffffffff@gmail.com']