![](/img/trans.png)
[英]how to get specific data key in json response from api twitter in python
[英]How to get specific key from Gmail API json with Python?
在 Python Gmail API 文檔中,get 函數提供了一個 json 輸出,我可以訪問“片段”數據。 我想訪問“標題”鍵的“值”信息。 我覺得這應該只是獲取多級 JSON 數據,但它正在逃避我。 有人可以幫我嗎?
http://googleapis.github.io/google-api-python-client/docs/dyn/gmail_v1.users.messages.html#get
{ # An email message.
**"headers": [ # List of headers on this message part. For the top-level message part, representing the entire message payload, it will contain the standard RFC 2822 email headers such as To, From, and Subject.**
{
"name": "A String", # The name of the header before the : separator. For example, To.
**"value": "A String", # The value of the header after the : separator. For example, someuser@example.com.**
},
],
"snippet": "A String", # A short part of the message text.
"raw": "A String", # The entire email message in an RFC 2822 formatted and base64url encoded string. Returned in messages.get and drafts.get responses when the format=RAW parameter is supplied.
"sizeEstimate": 42, # Estimated size in bytes of the message.
"threadId": "A String", # The ID of the thread the message belongs to. To add a message or draft to a thread, the following criteria must be met:
# - The requested threadId must be specified on the Message or Draft.Message you supply with your request.
# - The References and In-Reply-To headers must be set in compliance with the RFC 2822 standard.
# - The Subject headers must match.
"labelIds": [ # List of IDs of labels applied to this message.
"A String",
],
"id": "A String", # The immutable ID of the message.
}
for message in messages:
msg = service.users().messages().get(userId='me', id=message['id']).execute()
print(msg['snippet']) # This works
print(msg['value']) # This doesn't and I tried ['headers']['value'] also.
print("\n")
time.sleep(1)
Traceback (most recent call last):
File "C:/Users/Notebook/PycharmProjects/Jarvis/Gmail.py", line 65, in <module>
main()
File "C:/Users/Notebook/PycharmProjects/Jarvis/Gmail.py", line 58, in main
print("New Message: " + msg['value'])
KeyError: 'value'
標題中存在鍵"value"
。 您可以使用msg["headers"][0]["value"]
訪問它
所以,我在 JSON 文件上做了一個漂亮的打印,我認為 Python Gmail API 是錯誤的。 我實際尋找的標題和相應數據位於“有效載荷”部分。 這是獲取數據的有效方法還是我做錯了?
for message in messages:
msg = service.users().messages().get(userId='me', id=message['id']).execute()
message_from = msg['payload']['headers'][4]
print("You have a new message from: " + message_from['value'])
print(msg['snippet'])
print("\n")
time.sleep(1)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.