使用python登录谷歌?

时间:2011-07-19 22:05:53

标签: python login

我对网络编程相当新,但为了它,我试图登录谷歌帐户不使用标准代码,但作为python应用程序,但它不可能这样做 以前有人试过这个吗?有人可以帮忙吗?

4 个答案:

答案 0 :(得分:22)

我创建了一个处理谷歌登录的python类,并且能够获得要求用户登录的任何谷歌服务页面:

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隐藏输入值并将其发送回google +登录名和密码。它需要模块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")

希望这有帮助

答案 1 :(得分:1)

虽然可能不完全是你在这里寻找的东西,但我发现了一些来自我的类​​似post的代码。


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()

print get_unread_msgs("put-username-here","put-password-here")

参考:
How to auto log into gmail atom feed with Python?

答案 2 :(得分: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()

答案 3 :(得分:0)

Python 3的2020年更新


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')