Login via the browser to facebook and google without using their APIs, using Python

為{幸葍}努か 提交于 2019-12-23 02:46:06

问题


Is there any way of to login via the browser to facebook and google, but without using the provided APIs? So far I have tried mechanize with cookielib, webbrowser, requests and selenium, but I did not get any satisfying results. The closest I got was to log in using mechanize + cookielib but via command line. I have an app and all what I want it to do is to open a browser and open either the facebook or the google page and to automatically log me in using the provided user name and password.

Note: if this can be done more easily in a different language, I am interested in those too.

Thanks in advance for your help!


回答1:


Log-In Facebook / Option #1 - Socket:

import urllib2,cookielib

def TryToLoginFB(username,password):
    opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cookielib.CookieJar()))
    url1 = "https://login.facebook.com"
    url2 = "https://login.facebook.com/login.php?login_attempt=1"
    data = "&email="+username+"&pass="+password
    socket = opener.open(url1)
    socket = opener.open(url2,data)
    return socket

socket = TryToLoginFB("not_gonna_tell_you@gmail.com","my_password")

if "logout" in socket.read():
    print "OK"
else:
    print "Error"

# or use 'socket' in order to do whatever you wanna do at this point...

Log-In Facebook / Option #2 - Browser:

from selenium import webdriver

def TryToLoginFB(username,password):
    browser = webdriver.Firefox()
    browser.get('https://www.facebook.com')
    browser.find_element_by_xpath('//input[@id="email"]').send_keys(username)
    browser.find_element_by_xpath('//input[@id="pass"]').send_keys(password)
    browser.find_element_by_xpath('//input[@value="Log In"]').click()
    return browser

browser = TryToLoginFB("not_gonna_tell_you@gmail.com","my_password")

if "logout" in browser.page_source:
    print "OK"
else:
    print "Error"

# or use 'browser' in order to do whatever you wanna do at this point...

In order to install Selenium for Python on your machine, run 'pip install selenium' from a command line.

Log-In Email / Option #1 - Socket:

import smtplib,ssl

def TryToLoginEM(username,password):
    server = Connect(username)
    try:
        server.login(username,password)
    except smtplib.SMTPException,error:
        print error
        Disconnect(server)
        return None
    return server

def Connect(username):
    serverName = username[username.index("@")+1:username.index(".")]
    while True:
        try:
            server = smtplib.SMTP(serverDict[serverName])
        except smtplib.SMTPException,error:
            print error
            continue
        try:
            server.ehlo()
            if server.has_extn("starttls"):
                server.starttls()
                server.ehlo()
        except (smtplib.SMTPException,ssl.SSLError),error:
            print error
            Disconnect(server)
            continue
        break
    return server

def Disconnect(server):
    try:
        server.quit()
    except smtplib.SMTPException,error:
        print error

serverDict = {
    "gmail"  :"smtp.gmail.com",
    "hotmail":"smtp.live.com",
    "yahoo"  :"smtp.mail.yahoo.com"
}

server1 = TryToLoginEM("your_email@gmail.com","your_password")
server2 = TryToLoginEM("your_email@hotmail.com","your_password")
server3 = TryToLoginEM("your_email@yahoo.com","your_password")

if server1 and server2 and server3:
    print "OK"
else:
    print "Error"

# or use 'server1/2/3' in order to do whatever you wanna do at this point...

Log-In Email / Option #2 - Browser:

Just follow the instructions for 'Log-In Facebook / Option #2'.

In order to find the xpath of the elements, open the page in a web-browser and inspect each element.




回答2:


If you are on Mac OS X, one potential solution is to combine Python with Applescript, via the appscript Python module:

https://pypi.python.org/pypi/appscript

For example, Clark Goble posted an example Python script that automatically fills out a form on FedEx's website:

http://www.libertypages.com/clarktech/?page_id=1570

The key is the Safari.do_JavaScript statements, which allow you to have the browser navigate the page's DOM and submit information via Javascript. (You could also do the whole thing in Applescript, but I've found using appscript is generally easier.)



来源:https://stackoverflow.com/questions/20926193/login-via-the-browser-to-facebook-and-google-without-using-their-apis-using-pyt

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!