我最近尝试使用python登录codeforces,但失败了。我成功地获取了csrf令牌,但无论何时发送请求,之后都会返回错误403:禁止请求,我非常感谢能够提供帮助的人:
import pyautogui
import clipboard
import requests
from bs4 import BeautifulSoup
import re
#user_name = input()
#psd = input()
base = "http://codeforces.com"
service_url = "{base}/{login}".format(base=base, login="enter")
dt = requests.get("http://codeforces.com/enter?back=%2F")
dt = dt.text
ss=BeautifulSoup(dt, 'html.parser')
print(ss)
token = ss.find(id='body').find(style = 'position: relative;').findNext(style = 'position: relative;')
token = token.find(id = 'pageContent')
print(token)
token = token.find(**{'class': 'enterPage'})
token = token.find(**{'class': 'roundbox'})
token = token.find('form')
print(token)
csrf_token = token.find(action = "").get('value')
print(csrf_token)
token = token.find(**{'class': 'table-form'})
token = token.find('input')
print(token)
ftaa = token.findNext('input').get('value')
print(ftaa)
bfaa = token.findNext('input').get('value')
print(bfaa)
print(csrf_token, ftaa, bfaa)
usr_name = input()
psd = input()
payload = {
'csrf_token': csrf_token,
'action': 'enter',
#'ftaa': ftaa,
#'bfaa': bfaa,
'handle': usr_name,
'password': psd
}
data = requests.post(service_url, data=payload)
data = data.text
soup = BeautifulSoup(data, 'html.parser')
soup = soup.prettify()
print(soup)
我认为您还需要在标头中添加csrf令牌
网站添加了以下代码:
相关问题 更多 >
编程相关推荐