使用python登录到codeforces(漂亮的汤和请求)

2024-10-04 07:26:04 发布

您现在位置:Python中文网/ 问答频道 /正文

我最近尝试使用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)



Tags: importtokeninputdatabasegetdtfind
1条回答
网友
1楼 · 发布于 2024-10-04 07:26:04

我认为您还需要在标头中添加csrf令牌

import requests
from bs4 import BeautifulSoup

base = "https://codeforces.com"
service_url = "{base}/{login}".format(base=base, login="enter")

s = requests.session()
dt = s.get(service_url)
dt = dt.text
ss = BeautifulSoup(dt, 'html.parser')
with open("login.html", "w") as file:
    file.write(str(ss))
csrf_token = ss.find_all("span", {"class": "csrf-token"})[0]["data-csrf"]
print(csrf_token)

usr_name = input("User name: ")
psd = input("Password: ")

headers = {
    'X-Csrf-Token': csrf_token
}
payload = {
'csrf_token': csrf_token,
'action': 'enter',
'handleOrEmail': usr_name,
'password': psd,
}
data = s.post(service_url, data=payload, headers=headers)
data = data.text
soup = BeautifulSoup(data, 'html.parser')
print(soup)
with open("output.html", "w") as file:
    file.write(str(soup))

logout = soup.select_one("a[href*=logout]")["href"]
data = s.get(base + logout)
soup = BeautifulSoup(data.text, "html.parser")
with open("logout.html", "w") as file:
    file.write(str(soup))

网站添加了以下代码:

$.ajaxSetup({ scriptCharset: "utf-8" ,contentType: "application/x-www-form-urlencoded; charset=UTF-8", headers: {
'X-Csrf-Token': Codeforces.getCsrfToken()
}});

相关问题 更多 >