通过请求登录网站时禁止响应

2024-09-29 23:26:13 发布

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

我正在尝试通过请求登录并收到禁止的响应。你知道吗

import requests
from bs4 import BeautifulSoup

header = {'Host': 'fetlife.com',
      'User-Agent': 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:56.0) Gecko/20100101 Firefox/56.0',
      'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
      'Accept-Language': 'en-US,en;q=0.5',
      'Accept-Encoding': 'gzip, deflate, br',
      #'Cookie': 'language=en; _fl_sessionid=???',
      'Connection': 'keep-alive',
      'Upgrade-Insecure-Requests': '1',
      'If-None-Match': 'W/"7d905c0faa3450522096dfbfaea7558a"',
      'Cache-Control': 'max-age=0',
       }

login_post_url = str('https://fetlife.com/users/sign_in')
internal_url = str('https://fetlife.com/home')


with requests.Session() as sesh:

    response = sesh.get(login_post_url, headers=header)
    html = response.content
    soup = BeautifulSoup(html, 'lxml')

    hidden_tags = soup.find_all("input", type="hidden")


payload = {'utf8': '/',
           'authenticity_token': soup.find(attrs={'name': 'authenticity_token'})['value'],
           'user[otp_attempt]': 'step_1',
           'user_locale': 'en',
           'user[login]': 'un',
           'user[password]': 'pw',
           }

    sesh.post(login_post_url, data=payload, headers=header)
    response = sesh.get(internal_url)
    html = response.text

    print(html)

Tags: importcomurlresponsehtmlloginpostrequests

热门问题