Python请求后无所事事

时间:2016-06-26 03:34:36

标签: python python-3.x python-requests

我正在使用requestscfscrape库登录https://kissanime.to/Login

'''Login to website'''
def login(self, usr, pw):
    login_url = 'https://kissanime.to/Login'
    sess = requests.Session()

    # login credentials
    payload = {
        'username': usr,
        'password': pw,
        'redirect': ''
    }

    # Creating cfscrape instance of the session
    scraper_sess = cfscrape.create_scraper(sess)
    a = scraper_sess.post(login_url, data=payload)
    print(a.text)
    print(a.status_code)

a.text为我提供了相同的登录页面 a.status_code给了我200

这意味着我的登录根本不起作用。我错过了什么吗?根据chrome的网络监视器,我也应该得到status code 302

POST DATA图片: enter image description here

2 个答案:

答案 0 :(得分:0)

我使用mechanicalsoup

解决了这个问题

代码:

import mechanicalsoup

'''Login to website'''
def login(self, usr, pw):
    login_url = 'https://kissanime.to/Login'

    # Creating cfscrape instance
    self.r = cfscrape.create_scraper()
    login_page = self.r.get(login_url)

    # Creating a mechanicalsoup browser instance with 
    # response object of cfscrape
    browser = mechanicalsoup.Browser(self.r)
    soup = BeautifulSoup(login_page.text, 'html.parser')

    # grab the login form
    login_form = soup.find('form', {'id':'formLogin'})

    # find login and password inputs
    login_form.find('input', {'name': 'username'})['value'] = usr
    login_form.find('input', {'name': 'password'})['value'] = pw

    browser.submit(login_form, login_page.url)

答案 1 :(得分:-1)

此内容来自请求文档:

  

许多需要身份验证的Web服务都接受HTTP Basic Auth。这是最简单的类型,并且Requests直接支持它。

import requests,json
'''Login to website'''
def login(self, usr, pw):
    login_url = 'https://kissanime.to/Login'
    sess = requests.Session()

    # login credentials
    payload = {
        'username': usr,
        'password': pw,
        'redirect': ''
    }

    # Creating cfscrape instance of the session
    scraper_sess = cfscrape.create_scraper(sess)
    a = scraper_sess.post(login_url, data=json.dumps(payload))
    print(a.text)
    print(a.status_code)

您必须将有效负载发送为JSON ..

{{1}}

参考:http://docs.python-requests.org/en/master/user/authentication/