Last active
August 26, 2022 16:49
-
-
Save Adizlois/b6b47845770ce4ba2508cf724b737b05 to your computer and use it in GitHub Desktop.
Google login using mechanize in python....
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import mechanize | |
import cookielib | |
#Dealing with Unicode encoding.... | |
import sys | |
reload(sys) | |
sys.setdefaultencoding('utf8') | |
#Emulate browser.... | |
br = mechanize.Browser() | |
# Set Cookie Jar so we can stay logged in... | |
cj = cookielib.LWPCookieJar() | |
br.set_cookiejar(cj) | |
#Other variables setting... | |
br.set_handle_equiv( True ) | |
br.set_handle_gzip( True ) | |
br.set_handle_redirect( True ) | |
br.addheaders = [('User-agent', 'Firefox')] | |
br.set_handle_referer( True ) | |
br.set_handle_robots( False ) | |
#Go for google login | |
br.open('https://accounts.google.com/Login') | |
#Choose form | |
br.form = list(br.forms())[0] | |
#Set value for "Email" | |
br["Email"] = "XXXXXX" | |
br.submit() | |
br.response().read() | |
#Set "Passwd" | |
br.form = list(br.forms())[0] | |
br["Passwd"] = "XXXXXX" | |
br.submit() | |
#Check that it worked... | |
br.open("https://mail.google.com/mail") | |
print br.response().read() | |
#Let´s take a look at the Cookies that we have just got... | |
print br.response().read() | |
for c in cj: | |
print c |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment