简体   繁体   中英

Python requests, can't log into a site

I am trying to use Python (3.2) requests to login to a site and navigate protected content on subsequent pages. However, when I login it seems to just leave me at the original login page (not navigating to the success page), and the subsequent page call is only showing the unprotected content. Can you please help me identify the bug in my code:

import requests
import sys

class login:
    def __init__(self):
        self.payload = None
        self.c = None

    def start(self,username,password,loginpage):
        self.payload = {'login':username,'password':password}
        self.loginpage = loginpage

    def login(self,url):
        self.c = requests.session()
        response = self.c.post(self.loginpage,data=self.payload)
        if response.status_code == 200:
            request = self.c.get(url)
            print(request.text)

if __name__ == '__main__':
    username = 'username'
    password = "password"
    loginpage = "https://www.clubfreetime.com/login/"
    nextpage = "http://www.clubfreetime.com/new-york-city-nyc/free-theater-performances-shows"
    login = login()
    login.start(username,password,loginpage)
    login.login(nextpage)

it looks like you didnt check if the post you made is ok ; you just continue with a get, so this call is not linked to the previous request and then it of course continues

for example :

def login(self,url):
    self.c = requests.session()
    # test if the post is ok with your business process
    if self.c.post(self.loginpage,data=self.payload) == whateveryourneed:
       request = self.c.get(url)
       print(request.text)

this answer is not THE answer but just a way to try to show you the step you seemed to miss

hope this help ;)

I figured out the problem. I needed to change self.payload to: self.payload = {'login':username,'password':password,'submit_login':'Login'}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM