获取gmail新邮件---python版


#! /usr/bin/python
# -*- coding: utf-8 -*-
#author newdongyuwei@gmail.com

import httplib2
from xml.dom.minidom import parseString

#https://user:password@mail.google.com/mail/feed/atom
gmail_feed_url = "https://mail.google.com/mail/feed/atom"
user="newdongyuwei"
password = "xxxxxx"

http = httplib2.Http()
http.add_credentials(user, password)#basic auth
resp, content = http.request(gmail_feed_url , "GET", body={}, headers={} )
print resp,content
dom = parseString(content)
feed_list = dom.getElementsByTagName('entry')
result = []
for feed in feed_list:
result.append(";".join([feed.getElementsByTagName("title")[0].firstChild.nodeValue.strip(),feed.getElementsByTagName("summary")[0].firstChild.nodeValue.strip(),feed.getElementsByTagName("name")[0].firstChild.nodeValue.strip()]))

print result
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要使用Python获取Gmail的电子邮件,你可以使用Google API客户端库。以下是一个简单的示例代码,可以帮助你开始: 首先,确保你已经安装了google-api-python-client库。你可以使用以下命令进行安装: ``` pip install google-api-python-client ``` 接下来,你需要创建一个Google API项目,并启用Gmail API。在创建项目后,你将获得一个客户端ID和客户端密钥。 ```python from google.oauth2.credentials import Credentials from google_auth_oauthlib.flow import InstalledAppFlow from googleapiclient.discovery import build # 设置API访问范围 SCOPES = ['https://www.googleapis.com/auth/gmail.readonly'] def authenticate_gmail(): """使用OAuth 2.0进行身份验证,并返回Gmail服务对象""" flow = InstalledAppFlow.from_client_secrets_file( 'credentials.json', SCOPES) creds = flow.run_local_server(port=0) return build('gmail', 'v1', credentials=creds) def get_emails(): """获取Gmail收件箱中的邮件列表""" service = authenticate_gmail() results = service.users().messages().list(userId='me', labelIds=['INBOX']).execute() messages = results.get('messages', []) if not messages: print('没有找到邮件.') else: print('最新一封邮件的信息:') message = service.users().messages().get(userId='me', id=messages[0]['id']).execute() print('主题:', message['subject']) print('发件人:', message['from']) print('内容:', message['snippet']) get_emails() ``` 在上面的代码中,你需要将 `credentials.json` 替换为你的客户端密钥文件的路径。该代码使用OAuth 2.0进行身份验证,并使用Gmail API获取收件箱中的邮件列表。然后,它打印出最新一封邮件的主题、发件人和摘要。 确保你已经启用了Gmail API,并拥有正确的客户端密钥文件。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值