使用python登录谷歌?

问题描述 投票:15回答:3

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

python login
3个回答
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隐藏输入值并将其发送回谷歌+登录名和密码。它需要模块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
投票

虽然可能不是你在这里寻找的东西,但我发现了一些来自我的类​​似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='%[email protected]' % 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?


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()
© www.soinside.com 2019 - 2024. All rights reserved.