通过浏览器登录到Facebook和谷歌,而无需使用其API,使用Python [英] Login via the browser to facebook and google without using their APIs, using Python
问题描述
有没有经由浏览器Facebook和谷歌,但没有使用提供的API登录什么办法?到目前为止,我已经试过机械化
与 cookielib
, web浏览器
, 要求
和硒
,但我没有得到任何满意的结果。我得到的最接近是登录使用机械化+ cookielib
,而是通过命令行。我有一个应用程序,所有我想要做的就是打开浏览器并打开了Facebook或谷歌的页面,并自动登录我在使用所提供的用户名和密码。
请注意:如果这可以更方便地在不同的语言来完成,我感兴趣的是这些呢。搜索结果
预先感谢您的帮助!
登录了Facebook /选项#1 - 插座:
进口urllib2的,cookielib高清TryToLoginFB(用户名,密码):
首战= urllib2.build_opener(urllib2.HTTPCookieProcessor(cookielib.CookieJar()))
为url1 =https://login.facebook.com
URL2 =https://login.facebook.com/login.php?login_attempt=1
数据=&放大器;电子邮件=+用户名+&放大器;通=+密码
插座= opener.open(URL1)
插座= opener.open(URL2,数据)
回插座插座= TryToLoginFB(not_gonna_tell_you@gmail.com,MY_PASSWORD)在socket.read如果退出():
打印OK
其他:
打印错误#或者为了做任何你想在这一点上确实使用了'插座'...
登录了Facebook /选项#2 - 浏览器:
硒进口的webdriver高清TryToLoginFB(用户名,密码):
浏览器= webdriver.Firefox()
browser.get('https://www.facebook.com')
browser.find_element_by_xpath('//输入[@ ID =电子邮件]')。send_keys(用户名)
browser.find_element_by_xpath('//输入[@ ID =通行证]')。send_keys(密码)
browser.find_element_by_xpath('//输入[@值=登录]')。点击()
返回浏览器浏览器= TryToLoginFB(not_gonna_tell_you@gmail.com,MY_PASSWORD)在browser.page_source如果退出:
打印OK
其他:
打印错误#或者为了做任何你想在这一点上做的用'浏览器'...
在为您的机器上安装Python的硒,运行命令行PIP安装硒'。
登录电子邮件/选项#1 - 插座:
进口的smtplib,SSL高清TryToLoginEM(用户名,密码):
服务器=连接(用户名)
尝试:
server.login(用户名,密码)
除了smtplib.SMTPException,错误:
打印错误
断开(服务器)
返回None
返回服务器高清连接(用户名):
服务器名=用户名[username.index(@)+ 1:username.index(。)
而真正的:
尝试:
服务器= smtplib.SMTP(serverDict [服务器])
除了smtplib.SMTPException,错误:
打印错误
继续
尝试:
server.ehlo()
如果server.has_extn(STARTTLS):
server.starttls()
server.ehlo()
除了(smtplib.SMTPException,ssl.SSLError),错误:
打印错误
断开(服务器)
继续
打破
返回服务器高清断开(服务器):
尝试:
server.quit()
除了smtplib.SMTPException,错误:
打印错误serverDict = {
Gmail的:smtp.gmail.com
Hotmail的:smtp.live.com
雅虎:smtp.mail.yahoo.com
}服务器1 = TryToLoginEM(your_email@gmail.com,your_password)
服务器2 = TryToLoginEM(your_email@hotmail.com,your_password)
服务器3 = TryToLoginEM(your_email@yahoo.com,your_password)如果Server1和Server2上和服务器3:
打印OK
其他:
打印错误为了做任何你想在这一点上做的#或使用服务器1/2/3'...
登录电子邮件/选项#2 - 浏览器:
只需按照说明登录了Facebook /选项#2。
为了找到元素的xpath,在web浏览器中打开的页并检查每个元素
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!
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.
这篇关于通过浏览器登录到Facebook和谷歌,而无需使用其API,使用Python的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!