Python 使用SendGrid API从Json请求获取特定列
我正在使用SendGrid API获取我的电子邮件状态Python 使用SendGrid API从Json请求获取特定列,python,json,api,email,sendgrid,Python,Json,Api,Email,Sendgrid,我正在使用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:/
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()
请求返回如下请求。。。但我只能从中提取所有返回的电子邮件。我将如何使用上述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
是一个dict列表。然后,您可以使用列表理解来选择每个dict的所有“电子邮件”:
>>> [d["email"] for d in res.json()]
['taqi.hass@cogilent.com', 'taqi.offfffffffffffff@gmail.com']
如果我必须收到一封电子邮件请求,那么假设我必须检查taqi是否正确。offfffffff@gmail.com在响应中出现,那么我将如何测试
taqi。offfff@gmail.com在[d[“email”]中,对于res.json()中的d