最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

json - Log in to the site using Python - Stack Overflow

programmeradmin4浏览0评论

I am successfully logging in to the website "status_code 200" But when I try to get this one from the next page, I get to the authorization page. What to do in this situation?

import requests
import fake_useragent
from bs4 import BeautifulSoup

session = requests.Session()

link_login = ";

user = fake_useragent.UserAgent().random
headers = {
    'User-Agent': user,
    'Content-Type': 'application/json'
}

login = '...'
password = '...'
data_login = {
    'login': login,
    'password': password,
    'remember': '1'
}

response = session.post(link_login, json=data_login, headers=headers)

if response.status_code == 200:
    json_data = response.json()

    token = json_data.get("token")

    if token:
        headers['Authorization'] = f'Bearer {token}'

        profile_info = "; 
        profile_response = session.get(profile_info, headers=headers)

        print(profile_response.text)
    else:
        print("Ошибка: Токен не получен.")
else:
    print(f"Ошибка авторизации: {response.status_code}, {response.text}")

I am successfully logging in to the website "status_code 200" But when I try to get this one from the next page, I get to the authorization page. What to do in this situation?

import requests
import fake_useragent
from bs4 import BeautifulSoup

session = requests.Session()

link_login = "https://gta5rp/api/V2/users/auth/login"

user = fake_useragent.UserAgent().random
headers = {
    'User-Agent': user,
    'Content-Type': 'application/json'
}

login = '...'
password = '...'
data_login = {
    'login': login,
    'password': password,
    'remember': '1'
}

response = session.post(link_login, json=data_login, headers=headers)

if response.status_code == 200:
    json_data = response.json()

    token = json_data.get("token")

    if token:
        headers['Authorization'] = f'Bearer {token}'

        profile_info = "https://gta5rp/user/report" 
        profile_response = session.get(profile_info, headers=headers)

        print(profile_response.text)
    else:
        print("Ошибка: Токен не получен.")
else:
    print(f"Ошибка авторизации: {response.status_code}, {response.text}")
Share Improve this question edited 12 hours ago ruohola 24.1k7 gold badges72 silver badges113 bronze badges asked 13 hours ago RahitRahit 92 bronze badges 3
  • 1 Some websites use CSRF tokens for security. You may need to extract and include these tokens in your subsequent requests. – Emad Kerhily Commented 12 hours ago
  • Just inspect those requests with some network monitor. – Jeyekomon Commented 12 hours ago
  • What is the HTTP status code after session.get? Do you actually get a token? – Adon Bilivit Commented 10 hours ago
Add a comment  | 

1 Answer 1

Reset to default -1

Try this:

import requests
import fake_useragent
from bs4 import BeautifulSoup

session = requests.Session()

link_login = "https://gta5rp/api/V2/users/auth/login"

user = fake_useragent.UserAgent().random
headers = {
    'User-Agent': user,
    'Content-Type': 'application/json'
}

login = '...'
password = '...'
data_login = {
    'login': login,
    'password': password,
    'remember': '1'
}

# Perform login
response = session.post(link_login, json=data_login, headers=headers)

if response.status_code == 200:
    json_data = response.json()
    token = json_data.get("token")

    if token:
        # Set the Authorization header for the session
        session.headers.update({'Authorization': f'Bearer {token}'})

        # Add additional headers if required by the website
        session.headers.update({
            'Referer': 'https://gta5rp/',
            'Origin': 'https://gta5rp',
            'X-Requested-With': 'XMLHttpRequest'
        })

        # Access the next page using the same session
        profile_info = "https://gta5rp/user/report"
        profile_response = session.get(profile_info)

        if profile_response.status_code == 200:
            print("Успешно перешел на страницу профиля")
            print(profile_response.text)
        else:
            print(f"Не удалось получить доступ к странице профиля: {profile_response.status_code}, {profile_response.text}")
    else:
        print("Ошибка: Токен не получен.")
else:
    print(f"Ошибка авторизации: {response.status_code}, {response.text}")
发布评论

评论列表(0)

  1. 暂无评论