使用Python登录Google?

20

我对web编程还比较新,但出于好奇,我正在尝试使用Python应用程序而非标准代码登录Google账户,但这似乎是不可能的。有人之前尝试过吗?有人能提供帮助吗?


什么应用程序?Gmail,Google+,Picasa,地图? - Dustin Davis
不是应用程序,而是Google账户页面在网页上。https://www.google.com/accounts/ - Mir Khashkhash
4个回答

28

我创建了一个Python类,用于处理Google登录,并能够获取任何需要用户已登录的Google服务页面:

class SessionGoogle:
    def __init__(self, url_login, url_auth, login, pwd):
        self.ses = requests.session()
        login_html = self.ses.get(url_login)
        soup_login = BeautifulSoup(login_html.content).find('form').find_all('input')
        my_dict = {}
        for u in soup_login:
            if u.has_attr('value'):
                my_dict[u['name']] = u['value']
        # override the inputs without login and pwd:
        my_dict['Email'] = login
        my_dict['Passwd'] = pwd
        self.ses.post(url_auth, data=my_dict)

    def get(self, URL):
        return self.ses.get(URL).text

这个想法是前往登录页面的GALX隐藏输入值,并将其发送回谷歌登录和密码。它需要使用requestsbeautifulSoup模块。

使用示例:

url_login = "https://accounts.google.com/ServiceLogin"
url_auth = "https://accounts.google.com/ServiceLoginAuth"
session = SessionGoogle(url_login, url_auth, "myGoogleLogin", "myPassword")
print session.get("http://plus.google.com")

希望这能有所帮助。


谢谢!这是一个非常聪明的处理所有隐藏元素的方法,我一定会把这个“技巧”放在我的工具箱里。 - MadRabbit
超级流畅,比玩正则表达式获取GALX要容易得多,让我的生活变得更轻松。 - dreyco676
1
你可以使用字典推导式使代码更加简洁美观: my_dict = {i.get('name'): i.get('value') for i in lst if i.get('value')} - Sebastian Nielsen
这个还有效吗?如果是,那应该使用哪些url_login和url_auth? - Noah Covey

1

尽管可能并不完全符合您在这里寻找的内容,但我在一个类似的post中找到了一些代码,它对我来说是可运行的。


import urllib2

获取未读消息(user, passwd): auth_handler = urllib2.HTTPBasicAuthHandler() auth_handler.add_password( realm='新邮件提醒', 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()

print get_unread_msgs("填写用户名","填写密码")

参考文献:
如何使用Python自动登录Gmail Atom Feed?


1

2020年更新,适用于Python 3:

import urllib.request

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


print(unread_messages('username', 'password'))

0
你可以使用Python的urllib、urllib2和cookielib库来进行登录。
import urllib, urllib2, cookielib

def test_login():
    username = '' # Gmail Address
    password = '' # Gmail Password
    cookie_jar = cookielib.CookieJar() 
    opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cookie_jar)) 
    login_dict = urllib.urlencode({'username' : username, 'password' :password}) 
    opener.open('https://accounts.google.com/ServiceLogin', login_dict) 
    response = opener.open('https://plus.google.com/explore')
    print response.read()

if __name__ == '__main__':
    test_login()

这个有 Python 3 版本的实现吗(不使用 urllib2)? - toryan

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接