简体   繁体   English

Python请求,无法登录网站

[英]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. 我正在尝试使用Python(3.2)请求登录到站点并在后续页面上导航受保护的内容。 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'} 我需要将self.payload更改为:self.payload = {'login':username,'password':password,'submit_login':'Login'}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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