0

I'm having issues logging into a website with Python. I just want to login to the site, then get the raw html of a page that you can only see when logged in so I can parse it with BeautifulSoup. I've tried using the answer at How to use Python to login to a webpage and retrieve cookies for later usage? but it doesn't seem to work.

I looked at the POST data required using LiveHeaders, and I think I'm setting it properly but my code just returns the login page.

Anyone know what I'm doing wrong?

import http.cookiejar
import urllib.request
import urllib.parse

username = 'username'
password = 'password'
_type = 'g'
vcode = ''

cj = http.cookiejar.CookieJar()
opener = urllib.request.build_opener(urllib.request.HTTPCookieProcessor(cj))
login_data = urllib.parse.urlencode({'username' : username, 'password' : password, 'type' : _type, 'vcode': vcode})
login_data = login_data.encode('ascii')
opener.open('http://passthepopcorn.me/login.php', login_data)
resp = opener.open('http://passthepopcorn.me/requests.php')
print(resp.read())
Community
  • 1
  • 1

1 Answers1

1

This may not answer your question, but in any case: I'd recommend that you use the "requests" module (which you'll have to install with pip install requests) rather than urllib. You will almost certainly wind up with code like this:

    import requests

    username = 'username'
    password = 'password'
    _type = 'g'
    vcode = ''

    login_response = requests.post('http://passthepopcorn.me/login.php',
                                  {'username' : username,
                                   'password' : password,
                                   'type' : _type, 'vcode': vcode})

    gold = requests.get('http://passthepopcorn.me/requests.php',
                         cookies={'PHPSESSID': login_response.cookies['PHPSESSID']})

    print(gold.text)

That might not work either, but it's almost certainly very close to working, and it's a hell of a lot easier to understand.

offby1
  • 6,767
  • 30
  • 45