1

I'm a longtime viewer and first-time asker here at stackoverflow, so excuse me for my newbieness.

I've been struggling with something for the past 2 hours now, looked up several questions like this but none really helped me with this problem.

What i've got so far:

*

import http.cookiejar
import urllib
url = 'https://www.facebook.com/login.php'
values = {'email' : 'example@gmail.com',
          'pass' : 'mypassword' }
data = urllib.parse.urlencode(values)
cookies =  http.cookiejar.CookieJar()
opener = urllib.request.build_opener(
urllib.request.HTTPRedirectHandler(),
urllib.request.HTTPHandler(debuglevel=0),
urllib.request.HTTPSHandler(debuglevel=0),
urllib.request.HTTPCookieProcessor(cookies))
binary_data = data.encode('ascii')
response = opener.open(url, binary_data)
the_page = response.read()
http_headers = response.info()

*

There are no errors, but what i'm trying to achieve is that it could somehow tell me if the login was successful or not. I tried if "Logout" in str(response.read()): which, sadly, didn't do anything. I also tried the try / except -method, which again, did nothing.

Any help would be greatly appreciated!

  • A website will generally return a 200 series status code if everything succeeds. I think above you'd do response.getcode() after the open attempt to see the status returned by the request. I think your code will probably fail as I THINK Facebook requires https connections w/ cert pinning and don't think urllib does that (I could be wrong, haven't used it in a while). You might want to try upgrade to requests library [Decent intro here](http://requests.readthedocs.io/en/master/) – Lost Sep 13 '16 at 16:52
  • @Lost thanks for the quick response, i'll give requests a try! – Michael Kofph Sep 13 '16 at 17:14
  • Requests seems better than urllib, but for Facebook i'm getting a kind-of-error; for Facebook, the form requires pass for password, but when i enter pass, it mixes up with the Python's own function 'pass'. What can i do? New code: import requests s = requests.session() login_data = dict(email='example@gmail.com', pass='mypassword') s.post('http://facebook.com/login.php', data=login_data) r = s.get('http://facebook.com') conten = r.content; if "invalid" in str(conten): print("fail") else: print("success") – Michael Kofph Sep 13 '16 at 17:24
  • Python shouldn't read something that is a string as a reserved word. Hard to read code in comments, but I think you are formatting your login data like a requests cookie. You probably want to use a post payload like [this question](http://stackoverflow.com/questions/11892729/how-to-log-in-to-a-website-using-pythons-requests-module) that will put your email and pass fields inside string literals and avoid Python trying to execute them. – Lost Sep 13 '16 at 18:06

0 Answers0