如何使用Python自动登录gmail atom提要?

2024-04-27 00:16:11 发布

您现在位置:Python中文网/ 问答频道 /正文

Gmail有一个很好的东西可以得到一个原子源:

def gmail_url(user, pwd):
    return "https://"+str(user)+":"+str(pwd)+"@gmail.google.com/gmail/feed/atom"

现在,当您在浏览器中执行此操作时,它将验证并转发您。但在Python中,至少我所尝试的是不正确的。

url = gmail_url(settings.USER, settings.PASS)
print url
opener = urllib.FancyURLopener()
f = opener.open(url)
print f.read()

不是正确转发,而是这样做:

>>> 
https://user:pass@gmail.google.com/gmail/feed/atom
Enter username for New mail feed at mail.google.com: 

真糟糕!我不应该再输入用户名和密码了!!我怎样才能像在我的web浏览器中那样在python中自动转发,这样我就可以在没有所有BS的情况下获得feed内容呢?


Tags: httpscomurlsettingsfeedgooglepwd浏览器
1条回答
网友
1楼 · 发布于 2024-04-27 00:16:11

你可以使用HTTPBasicAuthHandler,我尝试了以下操作,结果成功了:

import urllib2

def get_unread_msgs(user, passwd):
    auth_handler = urllib2.HTTPBasicAuthHandler()
    auth_handler.add_password(
        realm='New mail feed',
        uri='https://mail.google.com',
        user='%s@gmail.com' % user,
        passwd=passwd
    )
    opener = urllib2.build_opener(auth_handler)
    urllib2.install_opener(opener)
    feed = urllib2.urlopen('https://mail.google.com/mail/feed/atom')
    return feed.read()

相关问题 更多 >