我正在使用requests和cfscrape库登录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
答案 0 :(得分:0)
代码:
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/