python 使用python登录facebook

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2030652/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 23:36:57  来源:igfitidea点击:

Logging into facebook with python

python

提问by tehryan

If I run the following code 10 times in a row, it will work about half the time and fail the rest. Anyone know why?

如果我连续运行以下代码 10 次,它会运行大约一半的时间,其余的都会失败。有谁知道为什么?

import urllib2, cookielib, re, os, sys

class Facebook():
    def __init__(self, email, password):
        self.email = email
        self.password = password

        cj = cookielib.CookieJar()
        opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))
        opener.addheaders = [('Referer', 'http://login.facebook.com/login.php'),
                            ('Content-Type', 'application/x-www-form-urlencoded'),
                            ('User-Agent', 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.1.7) Gecko/20091221 Firefox/3.5.7 (.NET CLR 3.5.30729)')]
        self.opener = opener

    def login(self):
        url = 'https://login.facebook.com/login.php?login_attempt=1'
        data = "locale=en_US&non_com_login=&email="+self.email+"&pass="+self.password+"&lsd=20TOl"

        usock = self.opener.open('http://www.facebook.com')
        usock = self.opener.open(url, data)
        if "Logout" in usock.read():
            print "Logged in."
        else:
            print "failed login"
            print usock.read()
            sys.exit()

f = Facebook("[email protected]", "asdfasdf")
f.login()

回答by K. Murray

So I tried your code, and got it to log in once, then like you I had trouble logging in again. On a line before the 'if' statement, I added print usock.read() and ended up getting a bunch of html code. I then dropped that code into a notepad, saved it as an html file, and pulled it up. This is what's happening: Facebook gets suspicious that we're logging in from a computer program, and is waiting for us to verify that we're real by showing a captcha word. The program doesn't account for this, and prints "Failed login" when it's really more of a pending login.

所以我尝试了你的代码,并让它登录一次,然后像你一样我再次登录时遇到了麻烦。在 'if' 语句之前的一行中,我添加了 print usock.read() 并最终得到了一堆 html 代码。然后我将该代码放入记事本中,将其另存为 html 文件,然后将其拉出。这就是正在发生的事情:Facebook 怀疑我们正在通过计算机程序登录,并等待我们通过显示验证码来验证我们是否真实。该程序没有考虑到这一点,当它实际上更像是一个挂起的登录时,会打印“登录失败”。

回答by Robus

I've run into the same problem. The only solution I found was... that facebook mobile is more stable.

我遇到了同样的问题。我发现的唯一解决方案是... facebook mobile 更稳定。

class Acc:
    jar = cookielib.CookieJar()
    cookie = urllib2.HTTPCookieProcessor(jar)       
    opener = urllib2.build_opener(cookie)

    headers = {
        "User-Agent" : "Mozilla/5.0 (X11; U; FreeBSD i386; en-US; rv:1.8.1.14) Gecko/20080609 Firefox/2.0.0.14",
        "Accept" : "text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,text/png,*/*;q=0.5",
        "Accept-Language" : "en-us,en;q=0.5",
        "Accept-Charset" : "ISO-8859-1",
        "Content-type": "application/x-www-form-urlencoded",
        "Host": "m.facebook.com"
    }

    def login(self):
        try:
            params = urllib.urlencode({'email':'[email protected]','pass':'dempassword','login':'Log+In'})
            req = urllib2.Request('http://m.facebook.com/login.php?m=m&refsrc=m.facebook.com%2F', params, self.headers)
            res = self.opener.open(req)
            html = res.read()

            #print res.getheader('location').split('/')[3]

        except urllib2.HTTPError, e:
            print e.msg
        except urllib2.URLError, e:
            print e.reason[1]
        return False

    def fetch(self,url):
        req = urllib2.Request(url,None,self.headers)
        res = self.opener.open(req)
        return res.read()

bla = Acc()
bla.login()

Also, facebook mobile isn't filled up with ajax, so figuring out what requests to make is much easier.

此外,facebook mobile 没有充满ajax,因此弄清楚要发出的请求要容易得多。

回答by Abhranil

The fix worked for me, every time. Then I wanted to actually read the content of the page after logging in. For this, I added a 'print usock.read()' right after 'print 'Logged in'', but that returned an empty string. I guessed it's because usock is getting called twice, so I just modified the first call to usock:

每次修复都对我有用。然后我想在登录后实际阅读页面的内容。为此,我在 'print 'Logged in'' 之后添加了一个 'print usock.read()',但它返回了一个空字符串。我猜是因为 usock 被调用了两次,所以我只是修改了对 usock 的第一次调用:

    a = usock.read()
    if "logout" in a:
        print "Logged in."
        return a

This way there's only one call to usock, the first time, and it works. Instead of print a, I then used return a, because then I could directly use login() to return the page source. But please suit yourself.

这样,第一次只有一个调用 usock 的方法,并且它有效。然后我没有打印 a,而是使用 return a,因为这样我就可以直接使用 login() 来返回页面源。但请适合自己。

回答by Andrey Fedoseev

Maybe Facebook limits the maximum number of login attempts per minute. It's just a guess.

也许 Facebook 限制了每分钟的最大登录尝试次数。这只是一个猜测。

回答by Caio Rom?o

Okay, I think I found the answer by basically looking at the response data.

好吧,我想我基本上是通过查看响应数据找到了答案。

"Logout" does not exist - at least for me -, yet, "Log out" and "logout" does (the later appears as logout.php, which probably won't change on different locales). So, for your particular issue, just replace "Log out"with "logout"and you're all set.

“注销”不存在 - 至少对我而言 - 但是,“注销”和“注销”确实存在(后者显示为logout.php,在不同的语言环境中可能不会改变)。因此,对于您的特定问题,只需替换"Log out"为即可"logout"

Now, as to why it was working after the first try, I didn't bother to check

现在,至于为什么它在第一次尝试后有效,我没有费心去检查

Also, I'd suggest using urllib's urlencodefor sending your data to avoid nasty bugs when sending stuff with "&", ";" and other characters used for control. (The "@" from the e-mails should be encoded also, but it doesn't seem to break this particular case)

另外,我建议使用 urllib 的urlencode来发送您的数据,以避免在发送带有“&”、“;”的内容时出现令人讨厌的错误 和其他用于控制的字符。(电子邮件中的“@”也应该被编码,但它似乎并没有打破这种特殊情况)

Note: My test was basically changing the string and running it over a loop for some time, no issues were detected. If it does break for you, maybe Andreyis right.

注意:我的测试基本上是更改字符串并循环运行一段时间,未检测到任何问题。如果它确实为您带来了麻烦,也许安德烈是对的。